Code_ The Hidden Language of Computer Hardware and Software - Charles Petzold [47]
The adding machine that we'll build in this chapter will be big, clunky, slow, and noisy, at least compared to the calculators and computers of modern life. What's most interesting is that we're going to build this adding machine entirely out of simple electrical devices that we've learned about in previous chapters—switches, lightbulbs, wires, a battery, and relays that have been prewired into various logic gates. This adding machine will contain nothing that wasn't invented at least 120 years ago. And what's really nice is that we don't have to actually build anything in our living rooms; instead, we can build this adding machine on paper and in our minds.
This adding machine will work entirely with binary numbers and will lack some modern amenities. You won't be able to use a keyboard to indicate the numbers you want to add; instead you'll use a row of switches. Rather than a numeric display to show the results, this adding machine will have a row of lightbulbs.
But this machine will definitely add two numbers together, and it will do so in a way that's very much like the way that computers add numbers.
Adding binary numbers is a lot like adding decimal numbers. When you want to add two decimal numbers such as 245 and 673, you break the problem into simpler steps. Each step requires only that you add a pair of decimal digits. In this example, you begin with 5 plus 3. The problem goes a lot faster if you memorized an addition table sometime during your life.
The big difference between adding decimal and binary numbers is that you use a much simpler table for binary numbers:
+
0
1
0
0
1
1
1
10
If you actually grew up with a community of whales and memorized this table in school, you might have chanted aloud:
0 plus 0 equals 0.
0 plus 1 equals 1.
1 plus 0 equals 1.
1 plus 1 equals 0, carry the 1.
You can rewrite the addition table with leading zeros so that each result is a 2-bit value:
+
0
1
0
00
01
1
01
10
Viewed like this, the result of adding a pair of binary numbers is 2 bits, which are called the sum bit and the carry bit (as in "1 plus 1 equals 0, carry the 1"). Now we can divide the binary addition table into two tables, the first one for the sum bit:
+ sum
0
1
0
0
1
1
1
0
and the second one for the carry bit:
+ carry
0
1
0
0
0
1
0
1
It's convenient to look at binary addition in this way because our adding machine will do sums and carries separately. Building a binary adding machine requires that we design a circuit that performs these operations. Working solely in binary simplifies the problem immensely because all the parts of a circuit—switches, lightbulbs, and wires—can be binary digits.
As in decimal addition, we add two binary numbers column by column beginning with the rightmost column:
Notice that when we add the third column from the right, a 1 is carried over to the next column. This happens again in the sixth, seventh, and eighth columns from the right.
What size binary numbers do we want to add? Since we're building our adding machine only in our minds, we could build one to add very long numbers. But let's be reasonable and decide to add binary numbers up to 8 bits long. That is, we want to add binary numbers that can range from 0000-0000 through 1111-1111, or decimal 0 through 255. The sum of two 8-bit numbers can be as high as 1-1111-1110, or 510.
The control panel for our binary adding machine can look like this:
We have on this panel two rows of eight switches. This collection of switches is the input device, and we'll use it to "key in" the two 8-bit numbers.