Transcript
This is the same adder from last time. Not a single new part. Five... and minus three. Flip... and the lights say two. The adder can subtract. The secret is in how we write minus three. Let's find out what that is.
Last time, we built this: four full adders in a row, passing a carry from right to left. It can add any two numbers from zero to fifteen.
But real numbers can be negative. A temperature can drop below zero. An elevator can go below the ground floor. A circuit has no place to write a minus sign. It only has lights that are on or off. So the minus sign itself has to become a bit. The obvious idea: use the leftmost light for the sign.
Leftmost light off means plus. On means minus. The other three lights hold the size. So plus three is zero, zero, one, one. And minus three is one, zero, one, one. Looks reasonable. But there are two problems. First, zero shows up twice: plus zero, and minus zero. Two patterns for the same number is a waste, and it makes comparing numbers awkward. Second, try our adder on three plus minus three. The answer should be zero. Instead, the lights say one, one, one, zero. Read with the sign rule, that's minus six. To use this, we'd need a whole new circuit just for negative numbers. There's a better way.
Picture an old mechanical counter, showing zero, zero, zero, zero. Roll it back by one. It doesn't stop at zero. It wraps around to nine, nine, nine, nine. A binary counter does the same thing. Roll back one from all zeros, and you get all ones. So let's simply call that minus one. One more step back gives one, one, one, zero: minus two. Then minus three, and so on. Lay all sixteen patterns around a circle. On the right side, zero up to seven. On the left side, minus one down to minus eight. And look: the leftmost bit is still one for every negative number. We kept the sign light for free. Adding is just walking clockwise around the circle. Minus three sits thirteen steps from zero, so adding it means walking thirteen steps forward. On a circle of sixteen, thirteen steps forward lands exactly where three steps back would. From three, that's zero.
This way of writing negative numbers is called two's complement. Almost every computer you've ever used works this way. You don't need the circle to find it. There's a two-step recipe: flip every bit, then add one. Three is zero, zero, one, one. Flip every bit: one, one, zero, zero. Add one: one, one, zero, one. That's minus three. Why does it work? Add any number to its flipped version, and every column becomes one, with no carries. All ones is minus one. So the flipped version is minus one minus the number. Add one more, and you get exactly minus the number. And zero? Flip it to all ones, add one, and it rolls over to all zeros. The carry falls off the end. Only one zero. Problem solved.
Now subtraction is easy. A minus B is the same as A plus minus B. And minus B is just B flipped, plus one. So A minus B equals A, plus B flipped, plus one. Flipping is easy: that's what NOT gates do. But we want a circuit that can still add, too. Here's the trick. Remember X-OR? If one input is zero, the other passes straight through. If one input is one, the other comes out flipped. So put an X-OR on each of the B wires, and connect all of them to one control line. We'll call it subtract. When subtract is zero, B passes through unchanged and the circuit adds. When subtract is one, every bit of B gets flipped. What about the plus one? The first adder has a carry input that we never used. Connect the same control line to it. One wire. Flip, and add one, together. Five minus three. Five is zero, one, zero, one. Three, flipped, is one, one, zero, zero. Plus the one from the control line. The adders do their relay, and the result is one, zero, zero, one, zero. The extra carry on the left simply falls off, and we're left with zero, zero, one, zero. Two. At the start, we typed minus three in by hand. Now the circuit does the flipping itself: four X-OR gates and one control line turn our adder into an adder and subtractor.
There's one catch. Four bits can only hold sixteen patterns: minus eight up to seven. So what's six plus three? Zero, one, one, zero, plus zero, zero, one, one, gives one, zero, zero, one. Read as two's complement, that's minus seven. Not nine. On the circle, we walked past seven and fell over the edge into the negative side. This is called overflow. The fix is to notice it. Two positive numbers should never add up to a negative one. And two negatives should never give a positive. A few extra gates watch for exactly that, and switch on a warning light: the overflow flag. Most processors have one. Real computers use thirty-two or sixty-four bits, so the range is enormous. Thirty-two bits go past two billion. But it still happens. In twenty fourteen, one video on this very site got so many views that its counter was about to overflow, and it had to be upgraded to sixty-four bits.
So now our little circuit can add and subtract, with negative numbers, and it knows when the answer doesn't fit. But a computer does much more than add and subtract. It compares numbers. It does AND and OR on whole rows of bits. How does one circuit know which job to do at each moment? Next time: circuits that choose. See you there.
Next episode: circuits that choose — building an ALU.