in Programming in C
425 views
0 votes
0 votes

what is the output of the following programming????

and am confusing is unsigned int stores signed integer, and 

what is ‘ ’ this symbol ?? and what happened when assigning ‘~0’ to y????? and 

what will be printed when x,y are printing and how …?

can anyone tell me how it happened ..Thankyou…..!

#include<stdio.h>

int  main()

{

   unsigned int x = -1;

   int y = ~0;

   if (x == y)

      printf("same");

   else

      printf("not same");

printf("\n x is %u, y is %u", x, y);

   return 0;

}

 
in Programming in C
by
425 views

1 Answer

0 votes
0 votes
1) When you assign the sign bit to unsigned bit then first converted to 2s complement

-1 => 1| 1 where first 1 is sign and second represent value

Now applying sign bit extension rule so 1111...1 upto the size/capacity of x

2) y=~0 means flip bits and represent in 2s complement i.e -(0+1) = -1

3) x==y then from y=-1 it is converted to unsigned which is 1111.1 upto the size/capacity of y which is same as size taken by x

Hence x and y contains same value

so same is printed

And output in 64 bit compiler is 4294967295 4294967295 here also converting signed to unsigned for y
edited by

3 Comments

nice explanation.....,but am not understanding how 2's complement of 0 will -1. i think 2's complement of 0 is again 0.

0
0

https://stackoverflow.com/questions/791328/how-does-the-bitwise-complement-operator-tilde-work

read this how '~' working

Conclusion for ~ is that

~N = -(N+1)

0
0
okay thank you.......!
0
0