Computing

How a computer adds 7 + 1 using nothing but switches

30seconds
The 30-second version

A computer chip has no idea what numbers are. It only has switches: billions of transistors that are either on or off.

Yet wire a few of those switches into logic gates, and they can add. One XOR gate and one AND gate add two bits. Two of those plus an OR gate handle a whole column, carry included. Line four up and the carry ripples from one to the next, exactly like carrying the one on paper.

Flip the bits yourself and watch the carry travel across the circuit.

Deep dive · 5 min
Deep dive · 5 min

A computer only has switches

Inside a processor there is nothing that knows what "7" means. There are only transistors, and a transistor in a digital chip is used as a switch: a voltage on one terminal lets current flow between the other two, or stops it. A wire is either at high voltage or low. Call those 1 and 0, and you have everything a computer works with.

That is why computers count in binary. In decimal each column is worth ten times the one to its right. In binary each column is worth twice as much: 1, 2, 4, 8, 16 and so on. So 1011 means 8 + 0 + 2 + 1, which is eleven. Any whole number can be written this way using only the two states a switch can be in.

The question is how switches can add. The answer was worked out in 1937 by Claude Shannon, then a master's student at MIT. He showed that circuits of on/off relays behave exactly like Boolean logic, the algebra of true and false. In the last chapter of his thesis he drew a circuit that adds binary numbers.

Gates: switches wired into logic

Put two switches in a row and current only gets through if the first and the second are closed. That is an AND gate. Put them side by side and current gets through if either is closed: an OR gate. In modern chips each gate is a small group of transistors arranged so the output wire is pulled up to 1 or down to 0. The workhorse NAND gate ("not and") takes just four transistors.

The gate addition needs most is XOR, "exclusive or". Its output is 1 when exactly one input is 1, and 0 when both inputs match. XOR takes a few more transistors than AND, but it is still only a handful.

Adding one column: the half adder

Add two single bits and there are only four cases:

  • 0 + 0 = 0
  • 0 + 1 = 1
  • 1 + 0 = 1
  • 1 + 1 = 10 (that is "two": write 0, carry 1)

Look at the right-hand digit of each answer. It is 1 when exactly one input is 1. That is XOR. Now look at the carry. It is 1 only when both inputs are 1. That is AND. So one XOR gate and one AND gate, fed the same two wires, add two bits. This pair is called a half adder.

Adding a full column: the full adder

A half adder is enough for the rightmost column, but every other column has three things to add: a bit from each number plus the carry from the column to its right. That is what you do on paper too.

The fix is to use two half adders. The first adds the two bits. The second adds the carry to that result. If either half adder produced a carry, this column passes a carry to the left, so an OR gate joins the two carry wires. That is a full adder. The textbook CMOS version uses 28 transistors.

Chaining them: the ripple-carry adder

To add two 4-bit numbers, put four full adders side by side, one per column, and wire each carry output into the carry input of the next adder to the left. The rightmost carry input is tied to 0. The last carry output becomes the fifth bit (worth 16) of the answer. That is the whole machine. Try it below.

Things to try:

  • Start from 7 + 1. Every column produces a carry, and you can watch it travel the full width before the answer settles on 8.
  • Set both numbers to 1111. The answer, 30, needs five bits: the last carry spills out of the top.
  • Open a full adder and find its two half adders. The first one's XOR output is 1 when the column would pass on an incoming carry; its AND output is 1 when the column makes a carry of its own.

Why real chips don't wait for the ripple

The ripple-carry adder has a weakness you can see in slow motion. The leftmost column cannot give its final answer until the carry has passed through every column to its right. Each stage adds a couple of gate delays, so a 64-bit ripple adder is slow by processor standards.

Real processors use carry-lookahead designs. They compute, for every column at once, whether it will generate a carry (both bits are 1) or propagate one (exactly one bit is 1). Those are the AND and XOR outputs of the first half adder. Extra logic combines these signals in a tree, so all the carries are known after a few levels of gates instead of a long chain. It costs more transistors and saves time.

How many switches is that?

The 4-bit adder above, at 28 transistors per full adder, needs 112. Intel's 4004 of 1971, often called the first commercial microprocessor, was itself a 4-bit chip, and the whole thing had about 2,300 transistors.

The A17 Pro chip in an iPhone 15 Pro has 19 billion. That would be enough for roughly 680 million full adders, though of course most of a chip is memory, wiring and control, not adders. Nvidia's Blackwell B200, a data-centre processor, packs 208 billion onto two joined dies. Underneath all of it is the same idea: a switch that is on or off, wired so that 1 + 1 comes out as 10.

More in Computing

View all →
Why swapping cards at random is not a fair shuffle: Fisher–Yates and the 27 paths Shuffling three cards by swapping each one with a random card from the whole row looks fair, but it is not. It makes 3 × 3 × 3 = 27 equally likely paths for… Computing4 min read How to find one name among a billion in 30 looks: binary search Pick a number from 1 to 1,000,000 and you can find it in at most 20 yes-or-no questions. The trick is binary search: look in the middle of a sorted list, throw… Computing4 min read Why common letters get short codes in a ZIP file: Huffman coding ASCII spends 8 bits on every letter, whether it is a common e or a rare z. In 1951 a graduate student, David Huffman, found a way to give frequent symbols… Computing4 min read How a computer finds one flipped bit and fixes it: Hamming's code A bit can flip in memory, on a disk or in a radio signal, and nothing tells you which one went wrong. In 1950 Richard Hamming published a code that finds the… Computing4 min read Why 0.1 + 0.2 isn't 0.3: flip the 64 bits a computer uses for decimals Type 0.1 + 0.2 into almost any programming language and you get 0.30000000000000004. Nothing is broken. Computers count in powers of two, and a tenth is an… Computing3 min read How a Bloom filter says "definitely not" using just a few bits How can a computer check whether a word is on a huge list using only a few bits per entry? A Bloom filter flips a handful of switches for every word you add… Computing3 min read