Code_ The Hidden Language of Computer Hardware and Software - Charles Petzold [49]
Let's review what we know so far. Adding two binary numbers produces a sum bit and a carry bit:
You can use the following two logic gates to get these results:
The sum of two binary numbers is given by the output of an XOR gate, and the carry bit is given by the output of an AND gate. So we can combine an AND gate and an XOR gate to add two binary digits called A and B:
And instead of drawing and redrawing an AND gate and an XOR gate, you can simply draw a box like this:
This box is labeled Half Adder for a reason. Certainly it adds two binary digits and gives you a sum bit and a carry bit. But the vast majority of binary numbers are longer than 1 bit. What the Half Adder fails to do is add a possible carry bit from a previous addition. For example, suppose we're adding two binary numbers like these:
We can use the Half Adder only for the addition of the rightmost column: 1 plus 1 equals 0, carry the 1. For the second column from the right, we really need to add three binary numbers because of the carry. And that goes for all subsequent columns. Each subsequent addition of two binary numbers can include a carry bit from the previous column.
To add three binary numbers, we need two Half Adders and an OR gate, wired this way:
To understand this, begin with the A and B inputs to the first Half Adder at the left. The output is a sum and a carry. That sum must be added to the carry from the previous column, so they're inputs to the second Half Adder. The sum from the second Half Adder is the final sum. The two Carry Outs from the Half Adders are inputs to an OR gate. You might think another Half Adder is called for here, and that would certainly work. But if you go through all the possibilities, you'll find that the Carry Outs from the two Half Adders are never both equal to 1. The OR gate is sufficient for adding them because the OR gate is the same as the XOR gate if the inputs are never both 1.
Instead of drawing and redrawing that diagram, we can just call it a Full Adder:
The following table summarizes all the possible combinations of inputs to the Full Adder and the resultant outputs:
A In
B In
Carry In
Sum Out
Carry Out
0
0
0
0
0
0
1
0
1
0
1
0
0
1
0
1
1
0
0
1
0
0
1
1
0
0
1
1
0
1
1
0
1
0
1
1
1
1
1
1
I said early on in this chapter that we would need 144 relays for our adding machine. Here's how I figured that out: Each AND, OR, and NAND gate requires 2 relays. So an XOR gate comprises 6 relays. A Half Adder is an XOR gate and an AND gate, so a Half Adder requires 8 relays. Each Full Adder is two Half Adders and an OR gate, or 18 relays. We need 8 Full Adders for our 8-bit adding machine. That's 144 relays.
Recall our original control panel with the switches and lightbulbs:
We can now start wiring the switches and lightbulbs to the Full Adder.
First connect the two rightmost switches and the rightmost lightbulb to a Full Adder:
When you begin adding two binary numbers, the first column of digits that you add is different. It's different because every subsequent column might include a carry bit from the previous column. The first column doesn't include a carry bit, which is why the carry input to the Full Adder is connected to ground. That means a 0 bit. The addition of the first pair of binary digits could, of course, result in a carry bit. That carry output is an input to the next column.
For the next two digits and the next lightbulb, you use a Full Adder wired this way:
The carry output from the first Full Adder is an input to this second Full Adder. Each subsequent column of digits is wired the same way. Each carry output from one column is a carry input to the next column.
Finally the eighth and last pair of switches are wired to the last Full Adder:
Here the final carry output goes to the ninth lightbulb.
We're done.
Here's another way to look at this assemblage of eight Full Adders, with each Carry Out serving as input to the next Carry In:
Here's the complete 8-Bit