Thread: bit representation of negative numbers

1. bit representation of negative numbers

I was doing some bit operations and when i try to get the bit representation of a negative number say -1 on my Intel processor the value that i get is

11111111111111111111111111111111

I had this impression that the msb is used to store the sign of a number so i was expecting that only the 0 (the value for 1) and 31st (the sign bit)
bits should be 1 but then all the bits are 1. I am still unable to understand why is this the case?

2. computers store signed integers in 2's compliment
Originally Posted by Wikipedia
The two's complement of a binary number is defined as the value obtained by subtracting the number from a large power of two (specifically, from 2N for an N-bit two's complement). The two's complement of the number then behaves like the negative of the original number in most arithmetic, and it can coexist with positive numbers in a natural way.
A two's-complement system or two's-complement arithmetic is a system in which negative numbers are represented by the two's complement of the absolute value;[1] this system is the most common method of representing signed integers on computers.[2] In such a system, a number is negated (converted from positive to negative or vice versa) by computing its two's complement. An N-bit two's-complement numeral system can represent every integer in the range −2N-1 to +2N-1-1.
The two's-complement system has the advantage of not requiring that the addition and subtraction circuitry examine the signs of the operands to determine whether to add or subtract. This property makes the system both simpler to implement and capable of easily handling higher precision arithmetic. Also, zero has only a single representation, obviating the subtleties associated with negative zero, which exists in ones'-complement systems.
Which is a not very helpful way of saying that to change the sign, you invert every bit, then add 1.

3. Originally Posted by roaan
I was doing some bit operations and when i try to get the bit representation of a negative number say -1 on my Intel processor the value that i get is

11111111111111111111111111111111

I had this impression that the msb is used to store the sign of a number so i was expecting that only the 0 (the value for 1) and 31st (the sign bit)
bits should be 1 but then all the bits are 1. I am still unable to understand why is this the case?
Notice: the lowest negative number is:
10000000000000000000000000000000

and 2^32 is ONE MORE THAN the highest possible unsigned 32-bit number (since 0 is an unsigned int):
4294967296

4294967296 - 1 = 429467295, in binary:
11111111111111111111111111111111

4. Originally Posted by abachler
Which is a not very helpful way of saying that to change the sign, you invert every bit, then add 1.
Or, if you will, find the lowest significant 1 and invert all bits left of it.
Of course, with endianess and everything, that might not work out well in computers, but for calculation in your head, it certainly works out alright.

5. And furthermore, you should keep in mind that twos-complement isn't always the choice of encoding (though often enough it is), so if possible, you shouldn't base your code on any particular representation.