Chapter 5

Adding Two Numbers

Time to put it all together. Switches make gates, gates make numbers possible, and now gates are going to add two numbers — live, in front of you, using nothing but the AND/OR/XOR gates from Chapter 3.

Remember carrying the 1?

Think back to grade-school addition. Adding two digits sometimes produces a result too big for one column, so you write down one digit and carry the rest into the next column:

carry →  1
  2 7
+ 1 5
  4 2

7 + 5 = 12: write "2," carry the "1" into the next column. Binary addition works exactly the same way — it just carries a lot more often, since it runs out of digits after just "1."

A circuit that can do this for one column needs to handle three inputs (the two digits, plus any carry coming in from the column before it) and produce two outputs (the digit to write down, and any carry to pass along). Let's build that circuit one piece at a time.

Input A Input B Carry Sum (output)

Step 1: The Half Adder

Start simple — just add two single bits, A and B, with no incoming carry to worry about yet. Look closely at the two gates below: this is exactly the XOR and AND gates from Chapter 3, just relabeled.

Interactive — Half Adder
SUM CARRY
ABSUMCARRY
0000
0110
1010
1101

The XOR gate gives the digit to write down. The AND gate gives the carry (it's only 1 when 1+1=10 in binary — "write 0, carry 1").

Step 2: The Full Adder

A half adder has no way to accept a carry coming in from the previous column — so it only works for the very first column. Chain two half adders together, plus one more OR gate to combine their carries, and you get a full adder: it accepts A, B, and a carry-in, and produces a sum and a carry-out.

Interactive — Full Adder
HALF ADDER 1 S1 C1 Cin HALF ADDER 2 SUM C2 CARRY OUT

A and B feed the first half adder. Its sum (S1) becomes an input to the second half adder, along with the incoming carry. Either half adder's carry can trigger the final carry-out.

This is the whole building block. A full adder handles one column of binary addition, carry-in and carry-out included. To add whole numbers, we just need several of these lined up in a row — one per binary digit.

Step 3: Chain them together

Line up four full adders side by side, and wire each one's carry-out into the carry-in of the column to its left — exactly like carrying the 1 by hand. This is called a ripple-carry adder, because the carry "ripples" across the whole chain. Build two 4-bit numbers below, then hit Add and watch it ripple.

Interactive — 4-Bit Ripple-Carry Adder
Number A
= 0
Number B
= 0
Input A Input B Carry Sum (output)
carry in = 0 A B FULL ADDER place 1 A B FULL ADDER place 2 A B FULL ADDER place 4 A B FULL ADDER place 8
 

Addition, solved. Every calculation inside every computer is built from exactly this pattern: switches make gates, gates make adders and other circuits, and those circuits get repeated by the billions and run billions of times per second. Next up: how the exact same adder secretly handles subtraction too.