Electronics
Build a 2-bit adding machine from gates and flip-flops
A 2-bit adding machine adds the number on two switches to a running total every time a clock ticks. It's an adder and a register in a loop: the adder works out total + input, and two D flip-flops store the answer at each rising clock edge, ready to be the next total. You need just three XOR gates, one AND gate and two flip-flops.
The parts
- B1 and B0, two switches: the number to add, 0 to 3
- FF1 and FF2, two D flip-flops: FF1 holds bit 0 of the total (Q0), FF2 holds bit 1 (Q1)
- Three XORs and one AND: the adder
- A clock, one add per rising edge, and two LEDs or a display for the total
Step 1: bit 0 is a half adder
Bit 0 adds Q0 and B0, with nothing carried in. The sum bit is XOR and the carry is AND:
Wire Q0 and B0 to an XOR, and its output to FF1's D. Wire the same two signals to the AND: its output is the carry into bit 1.
Step 2: bit 1 adds three bits
Bit 1 adds Q1, B1 and that carry. An odd number of 1s makes the sum bit 1, which is two XORs in a row:
XOR Q1 with B1, XOR the result with the carry, and wire that to FF2's D. Bit 1's own carry out would need a third bit to land in, so it isn't built.
Step 3: one clock for both
Clock both flip-flops from the same clock, so both bits update at the same edge. Take Q0 and Q1 back to the adder's inputs, and out to the display, Q0 as the 1s and Q1 as the 2s.
Live experiment
Count the lemonade
Pip runs a lemonade stand. Two switches, B1 and B0, say how many cups he sold this tick, 0 to 3, and every Tick adds them to his till: a 2-bit register fed by an adder. The till keeps only two bits, so see what happens when it passes 3.
“Project: Adding Machine” is the project that ends Logic Grid, Electronics World 7. Free with an account. Lessons unlock in order, so you’ll do the 48 before it first.
Step 4: test it
Start from 0 with both flip-flops cleared, set the switches, and tick:
| Total before | Add | True sum | Shows |
|---|---|---|---|
| 0 | 1 | 1 | 1 |
| 1 | 1 | 2 | 2 |
| 2 | 1 | 3 | 3 |
| 3 | 1 | 4 = 100 | 0 |
| 3 | 2 | 5 = 101 | 1 |
| 2 | 0 | 2 | 2 |
Two bits hold only 0 to 3, so a sum of 4 or more wraps, keeping its lowest two bits. That's overflow. Adding 0 holds the total, because each tick stores the same value again.
An adder, a register and a clock are the core of every processor's datapath. Swap the adder for an ALU and you're most of the way to a simple CPU.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“Project: Adding Machine” is the project that ends Logic Grid, Electronics World 7. Free with an account. Lessons unlock in order, so you’ll do the 48 before it first.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: a half adder. Two bits and no carry in is exactly a half adder. Its sum, Q0 ⊕ B0, goes to bit 0's flip-flop, and its carry, Q0 · B0, goes on to bit 1.
- Why not “a single OR gate”? OR gets 1 + 1 wrong: it gives 1, but the sum bit should be 0 with a carry. And it makes no carry at all.
- Why not “a full adder”? A full adder adds three bits. Bit 0 has no carry coming in, so two inputs, a half adder, are enough.
Show the answer
Answer: CLK1. In a register, every flip-flop listens to the same clock, so both bits update at the same edge. Clocking one from the other is the counter's ripple wiring.
- Why not “an input switch”? A switch would update the bits only when someone flips it. The register must update on every tick of the add clock.
- Why not “the previous flip-flop's inverted output”? Clocking one flip-flop from another is how a ripple counter is wired. A register's flip-flops share one clock, so both bits update together.
Show the answer
Answer: a D flip-flop. A D flip-flop copies D at the rising edge and holds it until the next one. Gates only work out the new total. They can't remember it.
- Why not “an input switch”? The switches hold the number being added, not the total. The total lives in the flip-flops.
- Why not “an XOR gate”? An XOR works out a sum bit from its inputs right now. It can't keep a value once its inputs move on.
“Project: Adding Machine” is the project that ends Logic Grid, Electronics World 7. Free with an account. Lessons unlock in order, so you’ll do the 48 before it first.
Questions people ask
Which chips can I build the adding machine from?
Three common logic chips cover it: a 74HC86 (four XOR gates), a 74HC08 (four AND gates) and a 74HC74 (two D flip-flops). Tie the flip-flops' unused set and reset pins high, and use a debounced button or a slow clock for the ticks.
Why doesn't the new total race round the loop?
A D flip-flop copies D only at the clock's rising edge. Both bits capture the sum worked out from the old total, then update together, and the adder's new answer can't get in until the next edge.
How do I make it add bigger numbers?
Add one flip-flop and one full adder per extra bit, each full adder's carry out feeding the next one's carry in. Four bits count to 15, eight bits to 255.
Related topics
- Half adder vs full adderA half adder adds two bits; a full adder adds the carry too, so they chain.
- How a D flip-flop worksCopy D to Q at the clock's rising edge, then hold it: one bit of memory.
- What is an ALU, and how does it work?Work out every answer at once, then let the opcode pick one: the heart of a CPU.
- How to add binary numbersColumn by column, right to left, carrying when two 1s meet: the same sum every adder chip does.