How Do Computers Add? Part 3
Have you ever wondered how computers and calculators—both of which are nothing more than mindless boxes of plastic, wires, and other strange parts—manage to add numbers? And so quickly! Math Dude explains the final part of the story.
Jason Marshall, PhD
Listen
How Do Computers Add? Part 3
Every great story must come to an end. And the story of our quest to understand how computers and calculators add is no exception. Although it seems almost impossible, somehow these mindless boxes of plastic, wires, and other odds-and-ends work together to do our mathematical bidding. So far we’ve talked about binary numbers, Boolean algebra, logic gates, and how the XOR gate can be used for the first steps of addition. But we still haven’t managed to wrap up the thorny problem of understanding how computers use this stuff to solve actual real world addition problems. Until today..
Review: Exclusive OR
The crux of our story to this point is that the tiny electronic components known as logic gates can be used to perform Boolean operations on one or more input bits (which can be either 1 for TRUE or 0 for FALSE) and spit out a binary TRUE or FALSE output bit as the result. We’ve learned about the three fundamental logic gates that implement the AND, OR, and NOT operations of Boolean algebra, and last time we learned about the all important exclusive OR gate—aka XOR.
The first important thing to know about exclusive OR gates is that they return TRUE exclusively when one or the other of their input bits—but not both—are TRUE. That means that 0 XOR 1 and 1 XOR 0 both return 1 while 0 XOR 0 and (this is the kind of strange one) 1 XOR 1 both return 0. The second big thing to know about XOR is that these output values are exactly the same as the values of the right-most bit (called the “sum bit”) when adding two binary numbers. In other words, the right-most bit in the simple binary addition problems 0 + 0 = 0, 1 + 0 = 1, 0 + 1 = 1, and 1 + 1 = 10 match the output of the XOR operation applied to the two input bits. Which means that the XOR operation gives us the sum bit in binary addition.
Time Out!
Big congratulations if all of this makes perfect sense and you’re still with me. But please don’t worry if you’re finding some of this to be a bit confusing. This isn’t easy! After all, it’s taken a lot of very smart people a very long time to figure out. The good news is that we really are close to the finish line. If you’re a bit befuddled at this point, I highly encourage you to revisit the previous shows in this series—starting with episode 141 on binary numbers—before continuing on. And be sure to check out the handy graphics I’ve posted along with some of the shows, too!
Half Adders and Full Adders
So we now have a device—an XOR gate—that can take two bits and tell us the sum bit we get when adding them. All we have to do now is come up with a device that will give us the so-called “carry bit” (which is analogous to the carry digit in decimal addition). In other words, in the problem 1 + 1 = 10, we need to figure out a way to get the 1 in 10? As it turns out, it’s very simple: just use an AND gate. If you think about it, you’ll see that the combination of one XOR gate (to give us the sum bit) and one AND gate (to give us the carry bit) is a perfect machine for calculating all the sums of two 1-bit numbers we listed earlier. In fact, it’s such a perfect machine for this task that it’s exactly what’s inside your computers and calculators. It’s called a “half-adder.”
But hold on a second. If this machine is so perfect, why is it a half-adder? What’s with the “half?” Well, so far we’ve only been talking about adding two 1-bit numbers. But what if we wanted to add a third number to an initial result? And what if that initial result had a carry bit—as in the binary number 10? That’s where a so-called “full-adder” comes into play. A full-adder—which is basically just two half-adders cleverly stuck together—works just like a half-adder, except that it also adds the value of a carry bit to the two input bits. If we chain several of these full-adders together—so the output carry bit from one becomes the input carry bit to the next, and so on—we end up with what’s called a ripple carry adder which allows us to add not just two numbers, but as many as we want. Which is exactly what your computer and calculator does!
Wrap Up
And with that, our quest is complete. I hope you’ve enjoyed our journey into the world of binary numbers, Boolean algebra, logic gates, and computer addition. It really is a fascinating and complex topic, and it’s a bit of a miracle that it all works! If you want to learn more, check out this excellent YouTube video on the subject. And I also highly recommend the book Code by Charles Petzold a read. It’s a ton of fun.
OK, that’s all the math we have time for today. Remember to become a fan of the Math Dude on Facebook where you’ll find lots of great math posted throughout the week. If you’re on Twitter, please follow me there, too. Finally, please send your math questions my way via Facebook, Twitter, or email at mathdude@quickanddirtytips.comcreate new email.
Until next time, this is Jason Marshall with The Math Dude’s Quick and Dirty Tips to Make Math Easier. Thanks for reading, math fans!