Search

Search papers and lessons

Error-Correcting Code

Level 3 · 12 min read

An error-correcting code adds redundant bits chosen so that codewords sit far apart, letting a receiver find and repair flipped bits; the Hamming(7,4) code fixes any single-bit error.

On this page

  • Colour = topic mix
  • Discrete math
  • Linear algebra
  • Abstract algebra
  • ReadReady to readNot yet

TL;DR

An error-correcting code adds redundant bits to a message so that a receiver can notice, and often repair, bits that were flipped in transit or storage. The codewords are chosen to be far apart from one another, and the receiver picks the closest one. The linear codes studied here do this with simple arithmetic on bits. The Hamming(7,4) code adds three parity bits to four data bits, and it repairs any single flipped bit.

Builds on: Finite Field, Vector Space · Leads to: linear block codes, Linear Programming Bound

Where it's used

Computer memory. Many servers and workstations store extra check bits beside the data, so that a single bit flipped by electrical noise or a stray particle is corrected before the program reads it. Hamming codes are the classic design for this, and the same idea of check bits beside the data is used in larger codes today.

Printed QR codes. A QR code can still be scanned after a corner is torn or smudged. The printed symbol carries redundant data, so the scanner can rebuild what the damage hid.

Deep-space links. A spacecraft sends its data across a very noisy radio channel and cannot ask for a resend in time. The data are encoded before transmission so that the receiver on Earth can repair the errors that the noise causes.

How it works

The idea is easiest to follow with the Hamming(7,4) code. It takes four data bits and adds three parity bits, giving seven bits in total. Write the data as d1d2d3d4d_1 d_2 d_3 d_4. The message is a string of bits, each 0 or 1. The parity bits are computed with addition modulo 2, the clock arithmetic on bits in which 1+1=01 + 1 = 0, so that 1 plus 1 wraps back to 0.

  1. Encode the message 1011. Here d1=1d_1 = 1, d2=0d_2 = 0, d3=1d_3 = 1 and d4=1d_4 = 1. Compute the parity bits: p1=1+0+1=0p_1 = 1 + 0 + 1 = 0, p2=1+1+1=1p_2 = 1 + 1 + 1 = 1 and p3=0+1+1=0p_3 = 0 + 1 + 1 = 0. The codeword is 1011010. Check: each parity rule adds an even number of 1s once its own bit is included.
  2. Transmit, and suppose bit 3 flips. The receiver gets 1001010. Check: the third bit changed from 1 to 0, and nothing else changed.
  3. Compute three checks on the received word. Each check adds the bits at a fixed set of positions, modulo 2. Check 1 looks at positions 1, 2, 4 and 5: the bits are 1, 0, 1, 0, which sum to 2, so the check is 0. Check 2 looks at positions 1, 3, 4 and 6: the bits are 1, 0, 1, 1, which sum to 3, so the check is 1. Check 3 looks at positions 2, 3, 4 and 7: the bits are 0, 0, 1, 0, which sum to 1, so the check is 1. The three results form the syndrome (0,1,1)(0, 1, 1).
  4. Match the syndrome to a position. The syndrome (0,1,1)(0, 1, 1) is exactly the pattern of checks that a single error in position 3 produces. So the receiver flips bit 3 back, giving 1011010.
  5. Read the data. The first four bits are the message, so the receiver recovers 1011. Check: this matches the message in step 1.
flowchart LR
  A[Message 1011] --> B[Add three parity bits]
  B --> C[Send 1011010]
  C --> D[Noise flips bit 3]
  D --> E[Compute the three checks]
  E --> F{Checks all zero?}
  F -->|no| G[Syndrome picks the bit to flip]
  G --> H[Recover 1011]
  F -->|yes| H

The math

Definition: Binary linear code

A binary linear code of length nn and dimension kk is a linear code that is a subspace C⊆F2nC \subseteq \mathbb{F}_2^n with 2k2^k codewords. The Hamming distance d(x,y)d(x, y) between two words is the number of positions in which they differ. The minimum distance dd of CC is the smallest Hamming distance between two different codewords. For a linear code, dd equals the smallest number of 1s in any nonzero codeword. Such a code is written [n,k,d][n, k, d]. The Hamming(7,4) code is [7,4,3][7, 4, 3].

Step 1 in symbols: the encoder

Let d=(d1,d2,d3,d4)\mathbf{d} = (d_1, d_2, d_3, d_4) be the message. The codeword is c=(d1,d2,d3,d4,p1,p2,p3)\mathbf{c} = (d_1, d_2, d_3, d_4, p_1, p_2, p_3), where

p1=d1+d2+d4,p2=d1+d3+d4,p3=d2+d3+d4,p_1 = d_1 + d_2 + d_4, \qquad p_2 = d_1 + d_3 + d_4, \qquad p_3 = d_2 + d_3 + d_4,

and all sums are taken modulo 2. Step 1 of the walkthrough used d=(1,0,1,1)\mathbf{d} = (1, 0, 1, 1), which gives p1=0p_1 = 0, p2=1p_2 = 1 and p3=0p_3 = 0.

The same encoder can be written as a matrix product c=dG\mathbf{c} = \mathbf{d}G with G=[I4∣P]G = [I_4 \mid P], where I4I_4 is the 4×44 \times 4 matrix with 1s on its diagonal and 0s elsewhere, and

P=(110101011111).P = \begin{pmatrix} 1&1&0 \\ 1&0&1 \\ 0&1&1 \\ 1&1&1 \end{pmatrix}.

Step 2 in symbols: the parity-check matrix

Let HH be the 3×73 \times 7 matrix whose rows are the three parity rules, with a 1 wherever a position takes part in that check:

H=(110110010110100111001).H = \begin{pmatrix} 1&1&0&1&1&0&0 \\ 1&0&1&1&0&1&0 \\ 0&1&1&1&0&0&1 \end{pmatrix}.

A word x\mathbf{x} is a codeword exactly when HxT=0H\mathbf{x}^{\mathsf T} = \mathbf{0} (mod 2). For the codeword 1011010, each row sums to an even number over its positions, so the product is zero. The rank of HH is 3, and the codewords form the kernel of HH, a subspace of dimension 7−3=47 - 3 = 4. That is why the code has 24=162^4 = 16 codewords.

Step 3 in symbols: the syndrome

Let r=c+e\mathbf{r} = \mathbf{c} + \mathbf{e} be the received word, where e\mathbf{e} is the error pattern. The syndrome is s=HrT\mathbf{s} = H\mathbf{r}^{\mathsf T}. Since HcT=0H\mathbf{c}^{\mathsf T} = \mathbf{0}, it depends only on the error:

s=HeT.\mathbf{s} = H\mathbf{e}^{\mathsf T}.

If e\mathbf{e} has a single 1 in position ii, then s\mathbf{s} is column ii of HH. The seven columns are (1,1,0)(1,1,0), (1,0,1)(1,0,1), (0,1,1)(0,1,1), (1,1,1)(1,1,1), (1,0,0)(1,0,0), (0,1,0)(0,1,0) and (0,0,1)(0,0,1). These are all seven nonzero vectors of F23\mathbb{F}_2^3, each appearing once. So every single error gives a different nonzero syndrome, and the syndrome tells the receiver which bit to flip.

Step 4 in symbols: correction and its limit

The decoder flips the bit at the position whose column equals s\mathbf{s}. This corrects any single error. A code with minimum distance dd corrects up to tt errors whenever d≥2t+1d \ge 2t + 1. Here d=3d = 3, so t=1t = 1. With two errors, the syndrome is nonzero, so the error is noticed, but it equals the sum of two columns. Because every nonzero vector is a column, that sum matches a third column, and the decoder flips a wrong bit.

Worked examples

Example
1. Tiny case: the repetition code

Encode 0 as 000 and 1 as 111. The two codewords differ in all three positions, so the minimum distance is 3. Suppose the receiver gets 010.

  1. Count the 0s in 010. There are two 0s and one 1.
  2. Take the majority. The majority value is 0, so the decoder outputs 0.
  3. Interpretation: the single flipped bit, in position 2, was repaired. The code spends three bits to send one, which is a rate of 1/3. Hamming(7,4) is far more efficient, with a rate of 4/7.
Example
2. Standard case: encode 1101

Let d1=1d_1 = 1, d2=1d_2 = 1, d3=0d_3 = 0 and d4=1d_4 = 1.

  1. Compute p1=d1+d2+d4=1+1+1=3p_1 = d_1 + d_2 + d_4 = 1 + 1 + 1 = 3, which is 1 modulo 2.
  2. Compute p2=d1+d3+d4=1+0+1=2p_2 = d_1 + d_3 + d_4 = 1 + 0 + 1 = 2, which is 0 modulo 2.
  3. Compute p3=d2+d3+d4=1+0+1=2p_3 = d_2 + d_3 + d_4 = 1 + 0 + 1 = 2, which is 0 modulo 2.
  4. The codeword is 1101100.
  5. Check each row of HH: positions 1, 2, 4, 5 hold 1, 1, 1, 1, which sum to 4, so the check is 0. Positions 1, 3, 4, 6 hold 1, 0, 1, 0, which sum to 2, so the check is 0. Positions 2, 3, 4, 7 hold 1, 0, 1, 0, which sum to 2, so the check is 0. All checks pass.
  6. Interpretation: the parity rules produced a valid codeword, which is what the receiver expects to see with no errors.
Example
3. Realistic case: a flipped bit in stored data

A memory word holds the data 0110. Its parity bits are p1=0+1+0=1p_1 = 0 + 1 + 0 = 1, p2=0+1+0=1p_2 = 0 + 1 + 0 = 1 and p3=1+1+0=0p_3 = 1 + 1 + 0 = 0. The stored codeword is 0110110.

  1. A stray particle flips bit 5, which is p1p_1 (it was 1 and becomes 0). The read gives 0110010.
  2. Check 1 covers positions 1, 2, 4, 5: the bits are 0, 1, 0, 0, which sum to 1, so the check is 1.
  3. Check 2 covers positions 1, 3, 4, 6: the bits are 0, 1, 0, 1, which sum to 2, so the check is 0.
  4. Check 3 covers positions 2, 3, 4, 7: the bits are 1, 1, 0, 0, which sum to 2, so the check is 0.
  5. The syndrome is (1,0,0)(1, 0, 0), which is column 5 of HH. Flip bit 5 back to recover 0110110.
  6. Interpretation: the program reads 0110 unharmed. The memory controller performs this check on each read, so the error is invisible to software.

Try it: The received word is 0110111. Find the syndrome, the bit to flip and the data. Answer: check 1 covers positions 1, 2, 4, 5 with bits 0, 1, 0, 1, sum 2, so 0. Check 2 covers positions 1, 3, 4, 6 with bits 0, 1, 0, 1, sum 2, so 0. Check 3 covers positions 2, 3, 4, 7 with bits 1, 1, 0, 1, sum 3, so 1. The syndrome (0,0,1)(0, 0, 1) is column 7, so flip bit 7 to get 0110110. The data are 0110.

Common mistakes and tradeoffs

It is tempting to think that adding more bits always means fewer errors. In fact the extra bits only help when they are arranged so that codewords are far apart. Random extra bits can leave two codewords one bit apart, and then a single flip can turn one valid message into another.

It is tempting to think that a code which corrects one error corrects any number. In fact correcting tt errors requires minimum distance at least 2t+12t + 1. The Hamming(7,4) code has distance 3, so it corrects one error and fails silently on two.

Tradeoff: rate against distance. A code with more redundancy is more reliable but sends fewer useful bits per symbol. The repetition code has rate 1/3, and Hamming(7,4) has rate 4/7.

When to use it. Hamming codes suit settings where errors are rare and independent, such as a single flipped bit in memory. They are cheap to build and fast to decode.

When not to use it. When errors arrive in bursts, one code can lose several bits in a row. Designs for such channels spread the errors out or use stronger codes. The Hamming(7,4) code is too weak for that case.

History

Richard W. Hamming worked at Bell Labs in the late 1940s. The Wikipedia article on Hamming codes says he published the code in 1950, and it remains in use in applications such as ECC memory. Claude Shannon's theory of communication, in A Mathematical Theory of Communication (1948), set the other half of the picture. It showed that a channel has a limit, its capacity, beyond which no code can make the error rate arbitrarily small. Hamming's codes are simple and useful, but they are far from that limit. The search for codes that approach it occupied coding theory for decades.

Recap

  • An error-correcting code adds redundancy so that codewords are far apart.
  • A linear code over F2\mathbb{F}_2 is a subspace, and its codewords are the kernel of a parity-check matrix HH.
  • The syndrome HrTH\mathbf{r}^{\mathsf T} depends only on the error, and for a single error it equals a column of HH.
  • Minimum distance dd corrects tt errors when d≥2t+1d \ge 2t + 1. Hamming(7,4) has d=3d = 3, so it corrects one error.
  • Read next: Linear Block Code for the general linear form, and Linear Programming Bound for limits on how good a code can be.

Check yourself

QuestionWhat is the minimum distance of the repetition code that sends 0 as 000 and 1 as 111?Show answer
The two codewords differ in all three positions, so the minimum distance is 3.
QuestionEncode the message 0011 with the Hamming(7,4) parity rules. Give the full seven-bit codeword.Show answer
Here d1=0d_1 = 0, d2=0d_2 = 0, d3=1d_3 = 1 and d4=1d_4 = 1. The parity bits are p1=0+0+1=1p_1 = 0 + 0 + 1 = 1, p2=0+1+1=2≡0p_2 = 0 + 1 + 1 = 2 \equiv 0 and p3=0+1+1=2≡0p_3 = 0 + 1 + 1 = 2 \equiv 0. The codeword is 0011100.
QuestionWhy can the Hamming(7,4) code correct one error but not two?Show answer
Its minimum distance is 3. Correcting tt errors needs distance at least 2t+12t + 1, which gives t=1t = 1. With two errors the syndrome is nonzero, but it matches the sum of two columns of HH, which equals a third column, so the decoder flips the wrong bit.
QuestionA receiver gets 1101101 from a Hamming(7,4) encoder. What message was sent?Show answer
Take the received word 1101101. Check 1 covers positions 1, 2, 4, 5 with bits 1, 1, 1, 1, sum 4, so 0. Check 2 covers positions 1, 3, 4, 6 with bits 1, 0, 1, 0, sum 2, so 0. Check 3 covers positions 2, 3, 4, 7 with bits 1, 0, 1, 1, sum 3, so 1. The syndrome (0,0,1)(0, 0, 1) is column 7, so flip bit 7 to get 1101100. The message is 1101.

Sources