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.
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.
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
| Block | Bytes | Parity | Damaged | Limit | Result |
|---|
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
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
- The QR codes are real (versions 1 to 6, byte mode, all four levels) and match a reference encoder module for module. Larger versions, the numeric, alphanumeric and kanji modes, and the third mask-scoring rule are left out.
- The QR reader here is given the grid. Finding a code in a photograph, at an angle, in bad light, is a separate problem that the error correction does not help with.
- The “about 7, 15, 25, 30 %” figures are the share of bytes in each block that may be wrong. A blot does not hit the blocks evenly, so the damage a code survives depends on where it falls.
- The curve in part 3 uses ordinary numbers and looks for the parabola that most points agree on by trying every triple. Real decoders work in a finite field and find the damaged positions directly from the parity checks, as the one in parts 4 and 5 does.
- Beyond its limit a decoder usually notices and gives up, but now and then heavy damage lands close to a different valid codeword and is “repaired” to the wrong message. The page reports that when it happens.