I saw following code which prints output "Same" but i am having trouble in understanding this program. Please help me to understand the program.
int main()
{
unsigned int x = -1;
int y = ~0;
if(x == y)
printf("same");
else
printf("not same");
return 0;
}
How the output "Same" comes? Please help me about what happens here.
Unsigned int x = -1 has the bit flags (32 bits) :
11111111111111111111111111111111
int 0 has the bit flags (32 bits) :
00000000000000000000000000000000
~0 is the negate of 0 (bitwise) which is (32 bits) :
11111111111111111111111111111111
As a side note :
unsigned int x = -1;
is equivalent to
unsigned int x = UINT_MAX.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With