Code_ The Hidden Language of Computer Hardware and Software - Charles Petzold [48]
The rest of the adding machine will consist of logic gates wired together in various ways. The switches will trigger the relays in the logic gates, which will then turn on the correct lights. For example, if we want to add 0110-0101 and 1011-0110 (the two numbers shown in the preceding example), we throw the appropriate switches as shown on the following page.
The bulbs light up to indicate the answer of 1-0001-1011. (Well, let's hope so, anyway. We haven't built it yet!)
I mentioned in the last chapter that I'll be using lots of relays in this book. The 8-bit adding machine we're building in this chapter requires no fewer than 144 relays—18 for each of the 8 pairs of bits we're adding together. If I showed you the completed circuit in its entirety, you'd definitely freak. There's no way that anyone could make sense of 144 relays wired together in strange ways. Instead, we're going to approach this problem in stages using logic gates.
Maybe you saw right away a connection between logic gates and binary addition when you looked at the table of the carry bit that results from adding two 1-bit numbers together:
+ carry
0
1
0
0
0
1
0
1
You might have realized that this was identical to the output of the AND gate shown in the last chapter:
AND
0
1
0
0
0
1
0
1
So the AND gate calculates a carry bit for the addition of two binary digits.
Aha! We're definitely making progress. Our next step seems to be to persuade some relays to behave like this:
+ sum
0
1
0
0
1
1
1
0
This is the other half of the problem in adding a pair of binary digits. The sum bit turns out to be not quite as straightforward as the carry bit, but we'll get there.
The first thing to realize is that the OR gate is close to what we want except for the case in the lower right corner:
OR
0
1
0
0
1
1
1
1
The NAND gate is also close to what we want except for the case in the upper left corner:
NAND
0
1
0
1
1
1
1
0
So let's connect both an OR gate and a NAND gate to the same inputs:
The following table summarizes the outputs of these OR and NAND gates and compares that to what we want for the adding machine:
A In
B In
OR Out
NAND Out
What we want
0
0
0
1
0
0
1
1
1
1
1
0
1
1
1
1
1
1
0
0
Notice that what we want is 1 only if the output from the OR gate and the NAND gate are both 1. This suggests that these two outputs can be an input to an AND gate:
And that's it.
Notice that there are still only two inputs and one output to this entire circuit. The two inputs go into both the OR gate and the NAND gate. The outputs from the OR and NAND gates go into the AND gate, and that gives us exactly what we want:
A In
B In
OR Out
NAND Out
AND Out
0
0
0
1
0
0
1
1
1
1
1
0
1
1
1
1
1
1
0
0
There's actually a name for what this circuit does. It's called the Exclusive OR gate or, more briefly, the XOR gate. It's called the Exclusive OR gate because the output is 1 if the A input is 1 or the B input is 1, but not both. So, instead of drawing an OR gate, NAND gate, and AND gate, we can use the symbol that electrical engineers use for the XOR gate:
It looks very much like the OR gate except that it has another curved line at the input side. The behavior of the XOR gate is shown here:
XOR
0
1
0
0
1
1
1
0
The XOR gate is the final logic gate I describe in detail in this book. (A sixth gate sometimes shows up in electrical engineering. It's called the coincidence or equivalence gate because the output is 1 only if the two inputs are the same. The coincidence gate describes an output opposite that of the XOR gate, so this gate's symbol is the same as the XOR gate but