How do unsigned integers differ from signed integers? - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: How do unsigned integers differ from signed integers? (/thread-4365.html)
How do unsigned integers differ from signed integers? - ferum - Apr 10, 2006 05:48 PM
how do signed/unsigned variables work? I figured it was probably just a bit at the very left that determined the sign, so by that reasoning setting an int to 0x40000000 would set the integer to 2147483648. But it doesn't, 0x40000000 sets the integer to 1073741824. so that means that setting the integer to 0x40000000 is setting it to 00100000000000000000000000000000, instead of 0100000000000000000000000000000. so then setting the int to 0x80000000 would set it to 2147483648. Again, wrong. 0x80000000 sets the integer to -2147483648. So then how would you set the int to (+)2147483648? Also, setting an unsigned int to the same hexadecimal numbers yields the same decimal numbers,including the -2147483648!
Someone, please, Save me from this vortex of big numbers and confusion!
[If you have finished reading this post the first time and 1) understand it and 2) have not developed a headache, then you are a genius]
How do unsigned integers differ from signed integers? - akb825 - Apr 10, 2006 06:26 PM
Signed integers work by using two's complement notation. Basically, to take the negative of a number, you take the number, flip the bits, and add 1. Do the same to reverse the negative. This makes it very easy to do things such as arithmetic with negative numbers, which is one of the reasons why it's used. Here's some information on two's complement numbers.
Unsigned works by treating the bit pattern as a positive number, regardless of how large it gets. The reason why it seems to give you the same result is you are probably telling printf (or whatever print function you're using) to print out signed rather than unsigned values. Use %u for printf to explicitly print out the unsigned value.
How do unsigned integers differ from signed integers? - OneSadCookie - Apr 10, 2006 06:46 PM
First, make sure you're printing your unsigned integers as unsigned integers -- C allows implicit conversion between signed and unsigned, which makes negative signed ints into large unsigned ones, and large unsigned ones into negative ints. printf's %d format is for ints, %u is for unsigned ints.
The largest signed int is 0x7fffffff, which is 2147483647.
How negative numbers work: http://en.wikipedia.org/wiki/Two%27s_complement
took so long to type this that I got beaten by akb825...[/edit]
How do unsigned integers differ from signed integers? - ferum - Apr 11, 2006 01:52 PM
Thanks for the replies!