Binary arithmetic
Introduction
An important part of the use of logic circuits is for computing various mathematical operations such as addition, multiplication, trigonometric operations, etc.
We must therefore have a way of representing numbers as binary data.
Nonnegative integers
The easiest numbers to represent are the nonnegative integers. To see how this can be done, recall how we represent number in the decimal system. A number such as 2034 is interpreted as:
2*103 + 0*102 + 3*101 + 4*100
But there is nothing special with the base 10, so we can just as well use base 2. In base 2, each digit value is either 0 or 1, which we can represent for instance by false and true, respectively.
In fact, we have already hinted at this possibility, since we usually write 0, and 1 instead of false and true.
All the normal algorithms for decimal arithmetic have versions for binary arithmetic, except that they are usually simpler.
For adding two numbers, it suffices to notice that there is a carry of 1 whenever we add 0 and 1, or 1 and 1:
1 1 1
- - -
1 0 1 1
+ 1 0 0 1
———–
1 0 1 0 0
Subtraction is no harder:
10 10
– –
1 0 0 1
- 1 1 0
————–
0 0 1 1
For multiplying two number, the algorithm is simpler, since we only multiply by 0 (which gives all zeros), or 1 (which gives the original number):
1 1 0 1
* 1 0 1
———–
1 1 0 1
0 0 0 0
1 1 0 1
—————-
1 0 0 0 0 0 1
Finally, division is just repeated subtraction as in decimal arithmetic:
0 1 1 0
——–
1 0 | 1 1 0 1
—– 1 0
—
1 0
1 0
—
0 1
Negative integers
Things are easy as long as we stick to nonnegative integers. They become more complicated when we want to represent negative integers as well.
It may seem that we can do just what we do in decimal representation, i.e., indicate with a special sign whether the number is negative or not. In binary arithmetic, we could simply reserve one bit to determine the sign. In the circuitry for addition, we would have one circuit for adding two number, and another for subtracting two numbers. The combination of signs of the two inputs would determine which circuit to use on the absolute values, as well as the sign of the output.
While this method works, it turns out that there is one that is much easier to deal with by electronic circuits. This method is called the “two’s complement” method. It turns out that with this method, we do not need a special circuit for subtracting two numbers.
In order to explain this method, we first show how it would work in decimal arithmetic with infinite precision. Then we show how it works with binary arithmetic, and finally how it works with finite precision.
Posted in Computer Science, Information Technology, Computer Architecture, Computer Architecture |
