Error-correcting codes: why a torn QR code still scans
When you scan a scratched or partly covered QR code, play a scratched disc, or get photos from a space probe
What problem does it solve?
How can a message fix itself after some of its bits get damaged?
The big idea
Add extra 'check' bits cleverly, so every possible single error leaves a different fingerprint. In the Hamming(7,4) code, 3 parity bits guard 4 data bits, and each parity bit watches the positions whose binary number contains its bit (1, 2 or 4). If an error happens, the failed checks spell out the position of the broken bit in binary, so you just flip it back. QR codes use a stronger cousin, Reed–Solomon codes, which treat data as points on a polynomial: if you know enough points, you can rebuild the missing ones.
How it works, step by step
- 1. Number the 7 bit positions 1 to 7; put parity bits at positions 1, 2 and 4.
- 2. Parity bit 1 makes positions 1, 3, 5, 7 have an even number of 1s; bit 2 does 2, 3, 6, 7; bit 4 does 4, 5, 6, 7.
- 3. The receiver re-checks all three groups.
- 4. Add up the labels (1, 2, 4) of the groups that fail: that sum is the position of the flipped bit.
- 5. Flip it back and read the 4 data bits.
The key formula
- 1 if that parity check fails, 0 if it passes
- if all checks pass, the total is 0: no single-bit error
How fast is it?
Hamming(7,4) spends 3 extra bits per 4 and fixes any single flipped bit. QR codes let the maker choose a level: L, M, Q or H recover roughly 7%, 15%, 25% or 30% of damaged codewords, which is why logos can sit in the middle of a QR code.
Try it yourself
- Encode the data 1011 by hand, flip position 6, and use the checks to find it.
- Cover part of a QR code with your thumb and see how much you can hide before it stops scanning.
Fun fact
Richard Hamming invented his codes at Bell Labs around 1950 out of frustration: the computer ran his jobs over the weekend, and whenever it detected an error it simply gave up. 'If the machine can detect an error,' he asked, 'why can't it locate it and correct it?'