Well as far as i know that's not the way negative values are stored.
They are stored in the so called "twos-complement representation".
That is, a number has all bit flipped and 1 added.
Code:
For example, in a 16 bit integer, the number 19 is written
0000000000010011
Flipping the bits produces
1111111111101100
and finally, adding one gives
1111111111101101
Notice that this holds no matter how many bits are being used to represent the integer: If it were a 32 bit integer, we would simply have an additional 16 ones added to the left.
The reason of the two-complement representation is that it allows you to sum 2 signed integers without caring for the sign. With the method you described, it's necessary to test the sign bit before proceeding with sum, and to behave differently.
Code:
For example, imagine that you want to add the number 32 (0000000000100000) to -19:
0000000000100000
+1111111111101101
__________________
10000000000001101
This gives us the correct answer of 13
A detailed description is here:
http://class.et.byu.edu/ce571/notes/binary.html
bye
Bookmarks