why unsigned int 0xFFFFFFFF is equal to int -1?
Posted
by conejoroy
on Stack Overflow
See other posts from Stack Overflow
or by conejoroy
Published on 2009-12-07T21:48:28Z
Indexed on
2010/04/06
5:03 UTC
Read the original article
Hit count: 354
perhaps it's a very stupid question but I'm having a hard time figuring this out =)
in C or C++ it is said that the maximum number a size_t (an unsigned int data type) can hold is the same as casting -1 to that data type. for example see http://stackoverflow.com/questions/1420982/invalid-value-for-sizet
Why?? I'm confused..
I mean, (talking about 32 bit ints) AFAIK the most significant bit holds the sign in a signed data type (that is, bit 0x80000000 to form a negative number). then, 1 is 0x00000001.. 0x7FFFFFFFF is the greatest positive number a int data type can hold.
then, AFAIK the binary representation of -1 int should be 0x80000001 (perhaps I'm wrong). why/how this binary value is converted to anything completely different (0xFFFFFFFF) when casting ints to unsigned?? or.. how is it possible to form a binary -1 out of 0xFFFFFFFF?
I have no doubt that in C: ((unsigned int)-1) == 0xFFFFFFFF or ((int)0xFFFFFFFF) == -1 is equally true than 1 + 1 == 2, I'm just wondering why.
thanks!
© Stack Overflow or respective owner