How do unsigned integers differ from signed integers?
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]
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]
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.
http://en.wikipedia.org/wiki/Twos_complement
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.
http://en.wikipedia.org/wiki/Twos_complement
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.
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
[edit]took so long to type this that I got beaten by akb825...[/edit]
The largest signed int is 0x7fffffff, which is 2147483647.
How negative numbers work: http://en.wikipedia.org/wiki/Two%27s_complement
[edit]took so long to type this that I got beaten by akb825...[/edit]
Thanks for the replies!
Possibly Related Threads...
Thread:  Author  Replies:  Views:  Last Post  
Storing Integers as Doubles  WhatMeWorry  2  5,403 
Jun 29, 2011 10:33 AM Last Post: ThemsAllTook 

(unsigned) char, bitsets when using  + / *  wyrmmage  7  5,912 
Jul 21, 2007 04:01 PM Last Post: wyrmmage 