REDUNDANCY · CODES · REPAIR

How error correction works

A scratched CD still plays, a QR code with a logo over it still scans, and a space probe twenty billion kilometres away still gets its pictures home. The trick is the same every time: send more than the message, in a pattern that lets the receiver work out what was damaged and put it back. Everything on this page can be broken by clicking on it.

1 · Say it three times

The simplest code: send every bit three times and let the receiver take a vote. One flipped copy is outvoted by the other two. It works, but it triples the size, and two flips in the same group still fool it.

Click any bit to flip it. Each group of three is one bit of the message; a red frame marks a group where the vote goes the wrong way.

2 · Checks that overlap: the Hamming code

In 1950 Richard Hamming found a way to protect four bits with three extra ones instead of eight. Each extra bit makes the number of ones in its circle even. The circles overlap so that every bit sits in a different combination of them.

Flip one bit, and the circles it sits in turn odd. That combination points at exactly one position, so the receiver knows which bit to flip back. It never needs to know what the message was.

solid ring: message bitdashed ring: check bitsmall numbers: positions 1 to 7shaded circle: odd, so something in it is wrong

Click a bit to flip it in transit. Then flip a second one: the code now points confidently at the wrong bit. One error per seven bits is its limit, and this is the code inside server memory (ECC RAM) to this day.

3 · The idea behind Reed–Solomon: a curve through the data

Hamming works on single bits. Real damage comes in clumps: a scratch, a smudge, a burst of static. In 1960 Irving Reed and Gustave Solomon treated the message as numbers on a curve instead. Three numbers fix a parabola. Send seven points of that parabola instead of three, and the receiver can lose track of some and still find the one curve that most points agree on.

Drag any point up or down. Blue points are the message, yellow points are the extra ones. With seven points for a three-number message, any two can be wrong. Move a third and the receiver either gives up or, if your three happen to line up with two others, is fooled.

4 · Reed–Solomon on bytes

The real thing does the same with bytes instead of points on paper, using arithmetic in which the 256 byte values form a closed number system (so nothing ever rounds or overflows). The parity bytes below are computed exactly as in a QR code.

message byte parity byte wrong value, position unknown (click once) missing, position known (click twice)

A whole byte counts as one error whether one of its bits is wrong or all eight, which is why this code shrugs off clumps. Finding a wrong byte costs two parity bytes: one to locate it, one to fix it. A byte the receiver already knows is missing (an erasure) costs only one.

5 · A QR code you can damage

BlockBytesParityDamagedLimitResult

Paint on the code. The reader below it works from the black and white squares alone, the way a phone does once it has found the code: it reads the format bits, undoes the mask, collects the bytes, and runs Reed–Solomon on each block.

  • The three corner squares are how a camera finds the code and its orientation. They carry no data and are not protected: damage them and a phone may never get as far as decoding. (The reader here is told where the grid is, so it carries on.)
  • The format bits next to the corners say which level and mask were used. They are stored twice, each copy with its own small code that survives three wrong bits.
  • The mask flips data squares in a regular pattern so the code has no large blank areas or stripes that confuse a camera.
  • The data is split into blocks, each with its own parity bytes, and the blocks are shuffled together so that one blot is shared out among all of them.
  • A logo is simply damage the designer chose. It works because level H can lose nearly a third of its bytes.

6 · Spreading the damage: interleaving

A code that can fix two bytes per block is useless against a scratch that wipes out six in a row, unless those six belong to different blocks. So the blocks are dealt out like cards: first byte of each, then second byte of each. A long burst on the medium becomes a few scattered errors in every block. CDs do this so thoroughly that a 2 mm scratch is inaudible.

On the medium, in the order written:

Sorted back into four blocks, each able to repair two bytes:

7 · Where else it is used

CDs, DVDs, Blu-rayTwo layers of Reed–Solomon with interleaving between them. A CD can repair a burst of about 4000 bits, a scratch 2.5 mm long, and hide longer ones by guessing the missing sound.
Server memoryECC RAM stores 72 bits for every 64 using an extended Hamming code: it fixes any single flipped bit, from a cosmic ray for instance, and detects any two.
Disks and data centresRAID 5 keeps one extra disk holding the XOR of the others, so any one disk can die. Cloud storage uses Reed–Solomon across machines: 10 data pieces plus 4 parity pieces survive the loss of any 4.
Deep spaceVoyager's pictures from Saturn onwards came through Reed–Solomon wrapped around a convolutional code. The signal that arrives is far weaker than the noise in the receiver.
Wi-Fi, 5G, satellite TVModern links use LDPC and polar codes: thousands of overlapping parity checks, decoded by passing guesses back and forth until they agree. They get within a fraction of a decibel of the theoretical limit Claude Shannon proved in 1948.
Only detectingThe last digit of a barcode, an ISBN, a credit card or an IBAN is a check digit. It cannot repair anything; it only tells you to type the number again. Network packets carry a checksum for the same purpose and are simply sent again.

All of these rest on one idea, the distance between valid messages. If any two valid codewords differ in at least five places, then a codeword with two places damaged is still closer to the original than to any other, and the receiver can snap it back. Redundancy is what buys that distance; a good code buys a lot of it with few extra bits.

What is simplified