Mansori

Build an Adder From Logic Gates

About this lesson, for parents and teachers

Your phone does billions of additions a second, and it has no idea what a number is. It only has tiny switches called logic gates. Build the circuit that adds, gate by gate.

Ages 12–14 · about 9 minutes · builds on Logic Gates: AND, OR, NOT · leads to Inside a CPU: Fetch, Decode, Execute

Try this

  1. Tap each dashed gate to choose AND, OR, XOR or NAND. The truth table checks every input combination live.
  2. Tap A and B (or a truth table row) to send different bits through your circuit and watch the wires light up.
  3. Upgrade to a full adder: it adds A, B and a carry coming in from the column to the right.
  4. Chain four full adders and set A = 7 and B = 9. Watch the carry ripple all the way to the left.

How it works

Adding binary digits works like adding on paper. 0 + 0 = 0, 0 + 1 = 1, and 1 + 1 = 10 in binary: write 0, carry 1. So adding two bits gives two outputs: a sum bit and a carry bit. Look at the truth table and you will spot two gates hiding in it.

The sum is 1 when exactly one input is 1. That is the XOR gate (exclusive or), written a ^ b in code. The carry is 1 only when both inputs are 1. That is the AND gate, a & b. One XOR plus one AND is a half adder. It is called half because it cannot accept a carry from the previous column.

A full adder adds three bits: A, B and carry-in. It is two half adders in a row. The first adds A and B. The second adds that sum to the carry-in. If either half adder produced a carry, there is a carry out, so the two carries are combined with an OR gate.

To add bigger numbers, line up full adders, one per bit. Each one's carry-out wires into the next one's carry-in, exactly like carrying a 1 to the next column on paper. This is a ripple-carry adder. 0111 + 1001 makes the carry ripple through every column, giving 10000, which is 16. Real CPUs use clever carry-lookahead circuits so they do not wait for the ripple.

Where engineers use this

Every CPU and GPU contains an arithmetic logic unit (ALU) built from adders like this one, made of billions of transistors that act as logic gates. Even subtraction and multiplication are built on top of adders.

Words to know

Logic gate
A tiny circuit that takes bits in and puts one bit out according to a rule, like AND, OR or XOR.
XOR
Exclusive or: outputs 1 when exactly one of its two inputs is 1.
Half adder
An XOR and an AND gate that add two bits, giving a sum and a carry.
Full adder
A circuit that adds three bits (A, B and carry-in), built from two half adders and an OR gate.
Truth table
A table listing a circuit's output for every possible combination of inputs.

Talk about it

Goal: Wire XOR and AND gates into a circuit that adds binary numbers, just like a real chip.

  • When you add 27 + 15 on paper, where does carrying happen? How is that like the ripple adder?
  • Why does a 4-bit adder need five output lights?
  • Why would chip designers want to avoid waiting for a carry to ripple through 64 adders?

Without a screen: Make gates with people: each person holds a card saying AND, OR or XOR, and raises a hand (1) or not (0) based on the two people feeding them. Line up a half adder with four people (two inputs, an XOR and an AND) and test all four input combinations.

Curriculum links: CSTA K-12 2-CS-02 (combine hardware and software components), 3A-CS-02 (levels of abstraction), 2-DA-07 (binary representation); AP Computer Science Principles Topic 2.1 (binary numbers) and Topic 3.5 (Boolean expressions)

Questions

What is a half adder?
A half adder is a circuit that adds two single bits. It uses an XOR gate for the sum bit and an AND gate for the carry bit, so 1 + 1 gives sum 0 and carry 1.

What is the difference between a half adder and a full adder?
A half adder adds two bits. A full adder adds three: the two bits plus a carry coming in from the previous column. Full adders can be chained to add numbers of any length.

How do computers add numbers?
They store numbers in binary and add them with circuits made of logic gates. A chain of full adders adds each column of bits and passes the carry to the next column, just like adding on paper.