Java Reference
In-Depth Information
Negative Binary Numbers
Another aspect to binary arithmetic that you need to understand is how negative numbers are represented.
So far you have assumed everything is positive — the optimist's view, if you will — that your glass is still
half full. But you can't avoid the negative side of life forever — the pessimist's perspective that your glass
is already half empty. How do you indicate a negative number? Well, you have only binary digits at your
disposal, so they must contain the solution.
For numbers where you want to allow the possibility of negative values (referred to as signed numbers)
you must first decide on a fixed length (in other words, fix the number of binary digits in a number) and
then designate the leftmost binary digit as a sign bit. You have to fix the length to avoid any confusion about
which bit is the sign bit as opposed to other 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 neg-
ative — indicated by the sign bit being 1.
Of course, you can have some numbers with 8 bits, and some with 16 bits, or whatever number of bits
you like, as long as you know what the length is in each case. If the sign bit is 0 the number is positive, and if
it is 1, the number is negative. This would seem to solve the problem, but not quite. If you add −8 in binary
to +12 you would really like to get the answer +4. If you carry out that operation simplistically, just putting
the sign bit of the positive value to 1 to make it negative, and then doing the arithmetic with conventional
carries from one bit position to the next on the left, it doesn't quite work:
12 in binary is 0000 1100
−8 in binary you suppose is 1000 1000
Because +8 is 0000 1000, the binary representation for −8 is the same, but with the leftmost bit set to 1.
If we now add these together we get:
12 + (−8) is 1001 0100
The value 1001 0100 seems to be −20 according to the rules, which is not what you wanted at all. It's def-
initely not +4, which you know is 0000 0100. “Ah,” I hear you say, “you can't treat a sign just like another
digit.” But that is just what you do have to do when dealing with computers because, dumb things that they
are, they have trouble coping with anything else. So you really need a different representation for negative
numbers if the same process for addition is to work regardless of the sign of the operands. Well, because the
same process for arithmetic operations should work regardless of the signs of the operands, you could try
subtracting +12 from +4 and see what you get. Whatever the result is should be −8:
+4 is 0000 0100
Take away +12 0000 1100
and you get
1111 1000
For each digit from the fourth from the right onward you 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 much like
it, it really is. Just try adding it to +12 or +15 in binary and you see that it works. So what is it? It turns out
that the answer is what is called the 2 ' s complement representation of negative binary numbers.
Now here I am going to demand a little faith on your part and avoid getting into explanations of why it
works. I'm just showing you how the 2's complement form of a negative number can be constructed from
a positive value and that it does work, so you can prove it to yourself. Let's return to the previous example
where you need the 2's complement representation of −8. We start with +8 in binary:
0000 1000
You now flip each binary digit — if it is one make it zero, and vice versa:
Search WWH ::




Custom Search