[LC++]int versus unsigned int

Andy Zivkovic andy at zivkotech.net.au
Thu Sep 27 09:46:19 UTC 2001


While I agree with what you said, I have a question about the following.

> Secondly, the argument that you can store double the ammount
> is dangerous: you don't want to rely on that.  You should
> consider an 'int' as exactly that: an integer, in the mathematical
> sense:
>
> -infinity ... -1, 0, 1, ... +infinity
>
> The 'unsigned int' looks like:
>
> ... 4294967293, 4294967294, 4294967295, 0, 1, ... +infinity

in 2's compliment

0 = 0x00000000
2^31-1 = 0x7FFFFFFF
-2^31 = 0x80000000
-1 = 0xFFFFFFFF

Therefore a signed int looks like
0, 1, ..., 2^31-1, -2^31, -2^31+1, ..., -1.

And an unsigned int looks like
0, 1, .., 2^31-1, 2^31, ..., 2^32-1


Now, I thought that almost every computer uses 2's compliment.

Of course, your point is still correct. many people would accidently do
things like 'for (unsigned int i = x; i > 0; i--)'. I have in the past :)




More information about the tuxCPProgramming mailing list