Mark R Rivet posted:
can someone tell me why on a signed int the range is -32768 to +32768.
why do we get one more in the negative. I understand it takes two
bytes of memory.
int is different sizes on different platforms.
Let's say it's 32-Bit. Here's what it looks like in memory:
0000 0000 0000 0000 0000 0000 0000 0000 0000
With an unsigned int , you can use all 32-Bits, that leaves you with:
2 ^ 32
possible combinations. If you include the number, 0, then that gives you a
range of:
0 -> (2 ^ 32) - 1
0 -> (4,294,967,296) -1
0 -> 4,294,967,295
If you have a signed int , then one of the bits is used to indicate the
sign. That leaves you with 31 Bits to express the value.
With those 31-Bits, there's:
2 ^ 31
possible combinations.
Include the number, 0, and that leaves you with:
(2 ^ 31) - 1 possible combinations, including 0.
(2 ^ 31) - 1 = 2,147,483,647
Now, to find out the range, just divide that by 2.
You get: 1,073,741,823.5
There's your problem, it's not an integer! The extra 1 is given to the
positive side.
--
I'll elaborate further, here's an 8-Bit Signed Integer:
0000 0000
The value of that is equal to 0.
Now let's make the negative:
1000 0000
0 = -0
Here, we have two values for 0, that's a waste! Therefore:
1000 0000
is equal to -1!
And concordantly:
1000 0001
is equal to -2!!
Hope that helps.
-JKop