Binary representation of a number -


below code binary representation of number. code works fine.....but don't know why if((x&(0x80000000))>0) should <0 instead of >0 because if first bit of x 1, number generated -2147483748, less 0 still code works.

#include<stdio.h> int main() {     int x;     scanf("%d",&x);     for(int i=0;i<32;i++)      {         if((x&(0x80000000))>0)             printf("1");         else             printf("0");          x=x<<1;     }     printf("\n");     getchar();     getchar();     return 0; } 

the type of hexadecimal constant, such 0x80000000, first of these types value fits in:

  • int
  • unsigned int
  • long int
  • unsigned long int
  • long long int
  • unsigned long long int

in c implementation int , unsigned int 32 bits, 0x80000000 not fit in int. unsigned int. then, in x & 0x80000000, x promoted int unsigned int match. thus, expression unsigned, , value greater zero, not less zero.


Comments

Popular posts from this blog

php - cannot display multiple markers in google maps v3 from traceroute result -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -