Java Reference
In-Depth Information
Negative Binary Numbers
There is another aspect to binary arithmetic that you need to understand - negative numbers. So far we
have assumed everything is positive - the optimist's view if you will - our glass is still half full. But we
can't avoid the negative side of life forever - the pessimist's perspective that our glass is already half
empty. How do we indicate a negative number? Well, we only have binary digits at our disposal and
indeed they contain the solution.
For numbers that we want to have the possibility of negative values (referred to as signed numbers) we
must first decide on a fixed length (in other words, the number of binary digits) and then designate the
leftmost binary digit as a sign bit. We have to fix the length in order to avoid any confusion about which
bit is the sign bit as opposed to bits that are digits. A single bit is quite capable of representing the sign
of a number because a number can be either positive - corresponding to a sign bit being 0, or negative
- indicated by the sign bit being 1.
Of course, we can have some numbers with 8 bits, and some with 16 bits, or whatever, as long as we
know what the length is in each case. If the sign bit is 0 the number is positive, and if it is 1 it is
negative. This would seem to solve our problem, but not quite. If we add -8 in binary to +12 we would
really like to get the answer +4. If we do that simplistically, just putting the sign bit of the positive value
to 1 to make it negative, and then doing the arithmetic with conventional carries, it doesn't quite work:
12 in binary is
0000 1100
-8 in binary we suppose is
1000 1000
since +8 is 0000 1000. If we now add these together we get:
1001 0100
This seems to be -20, which is not what we wanted at all. It's definitely not +4, which we know is 0000
0100. Ah, I hear you say, you can't treat a sign just like another digit. But that is just what we do have to
do when dealing with computers because, dumb things that they are, they have trouble coping with
anything else. So we really need a different representation for negative numbers. Well, we could try
subtracting +12 from +4 since the result should be -8:
+4 is
0000 0100
Take away +12
0000 1100
and we get
1111 1000
For each digit from the fourth from the right onwards we had to borrow 1 to do the sum, analogously to
our usual decimal method for subtraction. This supposedly is -8, and even though it doesn't look like it,
it is. Just try adding it to +12 or +15 in binary and you will see that it works. So what is it? It turns out
that the answer is what is called the two's complement representation of negative binary numbers.
Search WWH ::




Custom Search