views:

82

answers:

5

If on my compiler, int is of 16 bits, then it's range is -32768 to 32767(in a 2's complement machine).
I want to know why negative numbers have 1 extra no. i.e positive numbers go to 32767 but negative go to one more i.e.-32768.

How -32768 is represented on a 2's complement m/c?

+1  A: 

There isn't really any 'biasing'. The number is negative when the most significant bit is set. For "positive number space" (i.e, MSB is not set), as well as 1-32767, you have zero, hence the apparent lack of 32768.

-32768 would be represented by 0b1000000000000000. See link text

AJ
+5  A: 

On 16 bits you can fit pow(2,16) (2 to the power of sixteenth) different combinations to represent 65536 numbers. It was decided that zero looks best represented natively as 000...000 and positive numbers in "two's complement" system are normally readable (they're equal to so called "natural binary" representation like 0000 0000 0000 0101 = 5 decimal etc).

Negative numbers in two's complement start with 1111 1111 1111 1111 to represent -1. Think about it as a counter dial with numbers that goes 997, 998, 999 and suddenly when it has to represent 1000 it overflows and shows 000. The principle is the same here, but the direction is other way around - from ...000 to ...111. -2 is represented as 1111....1110 and so on.

Lowest possible number in two's complement will have 1 on front and zeroes on the rest of digits.

eyescream
So in 2's complement, is one bit reserved for sign ?I thought it is reserved only in sign-magnitude representation
Happy Mittal
No, not really reserved. The most left bit really holds value, 0 if it's set to 0, -32768 if it's set to 1. It can be used to check whether the number is positive or negative (exactly like sign-magnitude), but compiler will take care of it for you when you use signed/unsigned integers. If you're interested in it more, you can read about "sign extension"... It's a pretty funny trick that in 8 bit integers "-1" is "1111 1111" and in 16 bit it's "1111 1111 1111 1111" (identical most left signs don't matter, pretty much like "0000 0101" = "101" = 5).
eyescream
+3  A: 

If you're looking for a simple, down to earth answer:

There isn't any bias. There are equal amount of numbers on positive and negative side, positive numbers just start from 0 and negative from -1, so hence the difference of one. :)

Tatu Ulmanen
+1  A: 

There is no negative zero. (-0). Thats why it appears to be a bias. Really it is considered negative if the last bit is set. There is still another 7bits in a byte that can be set in both the positive and negative range.

acidzombie24
A: 

Another way to think of it is making a 1 bit signed variable. Signed means there must be negative numbers and of course there will be positive numbers. So you can have 1 positive one negative, what two numbers do you pic? now lets make it 2 bits, what 2 positive and negative do you pic? really the secret is the last bit represents negative.

Theres extra work if we want to consider 0 a negative number. and it would be wasteful to have a negative 0.

acidzombie24