About

3.4 Hamming Codes & Error Correction

Theory: Hamming Codes & Error Correction

Detecting an error is enough when the data can simply be sent again. In computer memory and long-term storage that is not possible: the original is gone. To keep data correct over the long term we need codes that can detect and correct errors. The two most important are Hamming codes (used for memory, our focus here) and Reed-Solomon codes, which are good at correcting burst errors (a series of adjacent damaged bits), which is why easily scratched CD-ROMs use a Reed-Solomon code.

Code words: m data bits + r check bits

A Hamming code forms code words by adding redundant check bits (parity bits) to each data word:

n=m+r{m=data bitsr=redundant (check/parity) bitsn=bits in the code wordn = m + r \qquad \begin{cases} m = \text{data bits} \\ r = \text{redundant (check/parity) bits} \\ n = \text{bits in the code word} \end{cases}

Hamming distance

The Hamming distance between two code words is the number of bit positions in which they differ. To compute it, XOR the words and count the 1s in the result.

Mini-example

  1 0 0 0 1 0 0 1
⊕ 1 0 1 1 0 0 0 1
  ---------------
  0 0 1 1 1 0 0 0   → three 1s → Hamming distance 3

Minimum Hamming distance, D(min)

The minimum Hamming distance D(min⁡)D(\min) of a code is the smallest Hamming distance between all pairs of legal code words. It determines what the code can do:

GoalRequired minimum distance
detect up to kk single-bit errorsD(min⁡)=k+1D(\min) = k + 1
correct up to kk single-bit errorsD(min⁡)=2k+1D(\min) = 2k + 1

Why? Think of legal code words as islands.

Turning the rules around: a code with distance D(min⁡)D(\min) detects D(min⁡)−1D(\min) - 1 errors and corrects ⌊(D(min⁡)−1)/2⌋\lfloor (D(\min) - 1)/2 \rfloor errors.

Mini-example (as in the lecture)

Two data bits + one even parity bit: the legal words are 000, 011, 101, 110, so D(min⁡)=2D(\min) = 2 → it detects k=1k = 1 error but corrects none. If 010 is received it has odd parity, so an error is detected. It cannot be corrected: 010 is distance 1 from 000, from 011 and from 110, so we cannot tell which was sent. A received 000 is legal and accepted, but it could still be a 2-bit error (e.g. 011 → 000), which this code cannot see.

How many check bits? (m+r+1)≤2r(m + r + 1) \le 2^r

Take any legal code word of nn bits. An error can hit any one of its nn bits, giving nn different illegal words at distance 1 (e.g. for the 3-bit word 100 they are 000, 110 and 101). So each legal word “owns” n+1n + 1 patterns: itself plus its nn one-bit-error neighbours. To correct single-bit errors these groups must not overlap. There are 2m2^m legal words and only 2n2^n patterns in total:

(n+1) 2m≤2n⟹(m+r+1) 2m≤2m+r⟹(m+r+1)≤2r(n + 1)\,2^m \le 2^n \quad\Longrightarrow\quad (m + r + 1)\,2^m \le 2^{m+r} \quad\Longrightarrow\quad \boxed{(m + r + 1) \le 2^r}

This gives the lower limit on the number of check bits. Find the smallest rr that satisfies it:

mm (data bits)smallest rrcheckn=m+rn = m + r
434+3+1=8≤84 + 3 + 1 = 8 \le 87
848+4+1=13≤168 + 4 + 1 = 13 \le 16 (r=3r = 3: 12≤812 \le 8 fails)12
16516+5+1=22≤3216 + 5 + 1 = 22 \le 3221
32632+6+1=39≤6432 + 6 + 1 = 39 \le 6438
64764+7+1=72≤12864 + 7 + 1 = 72 \le 12871

So an 8-bit data word that must survive any single-bit error needs 4 check bits, giving 12-bit code words.

Where the check bits go: the lecturer’s layout

  1. Number the bit positions 1 to n from the right.
  2. Put the check bits at the positions that are powers of 2: 1, 2, 4, 8, … .
  3. Fill the remaining positions with the data bits from the highest position down, in the order the data word is written (leftmost data bit → highest non-power-of-2 position).

For m=8m = 8, n=12n = 12:

position :  12  11  10   9   8   7   6   5   4   3   2   1
binary   : 1100 1011 1010 1001 1000 0111 0110 0101 0100 0011 0010 0001
contents :  d   d   d   d   P8  d   d   d   P4  d   P2  P1

Which positions each check bit covers

Write each position number in binary. The check bit at position cc covers every position whose binary number has the cc bit set.

Check bitBinaryCovers positions
P100011, 3, 5, 7, 9, 11 (all odd positions)
P200102, 3, 6, 7, 10, 11
P401004, 5, 6, 7, 12
P810008, 9, 10, 11, 12

Read the other way round, each position is covered by exactly the check bits that add up to its number: position 11=8+2+111 = 8 + 2 + 1 is covered by P8, P2 and P1. That is the whole trick: if position 11 goes wrong, exactly P8, P2 and P1 fail, and 8+2+1=118 + 2 + 1 = 11 points straight at it.

Generating the check bits (even parity)

Each check bit is the modulo-2 sum (XOR) of the data bits it covers, so its group (check bit + covered data bits) has an even number of 1s.

  1. Place the data bits.
  2. For each check bit cc: XOR the data bits in the positions it covers.
  3. Write the check bits in place. The key KK is the check bits read P8 P4 P2 P1.

m=4m = 4, data 1011 → r=3r = 3, n=7n = 7, data in positions 7, 6, 5, 3 = 1, 0, 1, 1. P1 (3, 5, 7) =1⊕1⊕1=1= 1 \oplus 1 \oplus 1 = 1; P2 (3, 6, 7) =1⊕0⊕1=0= 1 \oplus 0 \oplus 1 = 0; P4 (5, 6, 7) =1⊕0⊕1=0= 1 \oplus 0 \oplus 1 = 0. Code word (positions 7 → 1): 1 0 1 0 1 0 1 = 1010101.

Position121110987654321
TypeddddP8dddP4dP2P1
Bit
Check bitCovers data positionsData bits thereNumber of 1sValue
P13, 5, 7, 9, 11
P23, 6, 7, 10, 11
P45, 6, 7, 12
P89, 10, 11, 12

Locating and correcting a single-bit error

The error-correcting function in memory works like this: when a word is written, a function ff computes the key KK from the data MM and both are stored. When the word is read, ff is applied again to the data read out, and the new key is compared with the stored key:

Diagram loads as you scroll.

Procedure

  1. Recompute the check bits from the data bits that were read: K′K'.
  2. XOR with the stored key: syndrome =K⊕K′= K \oplus K' (read as a binary number P8 P4 P2 P1).
  3. Interpret the syndrome:
    • 0000 → no error.
    • a power of 2 (0001, 0010, 0100, 1000) → the error is in that check bit itself; the data is fine.
    • any other value → it is the position of the wrong data bit: flip it.

An equivalent way to get the same syndrome: for each check bit cc, XOR all bits of its group including the stored check bit. A group with even parity gives 0, a failed group gives 1, and the failed groups add up to the bad position.

A Hamming code with D(min⁡)=3D(\min) = 3 corrects one error. If two bits flip, the syndrome is non-zero but points at the wrong position, and “correcting” it makes things worse. Memory systems often add one more overall parity bit (SEC-DED: single-error-correct, double-error-detect) so that double errors are at least detected.


Worked Examples

Exercise 1

(a) Find the Hamming distance between 10001001 and 10110001. (b) Find D(min⁡)D(\min) of the code {00000,01011,10101,11110}\{00000, 01011, 10101, 11110\}. How many errors can it detect and how many can it correct?

Show solution ↓
Solution

(a) XOR and count the 1s:

  10001001
⊕ 10110001
  --------
  00111000   → 3 ones

Hamming distance 3.

(b) There are (42)=6\binom{4}{2} = 6 pairs:

PairXORDistance
00000, 01011010113
00000, 10101101013
00000, 11110111104
01011, 10101111104
01011, 11110101013
10101, 11110010113

D(min⁡)=3D(\min) = 3.

  • Detect: D(min⁡)=k+1⇒k=2D(\min) = k + 1 \Rightarrow k = 2 errors.
  • Correct: D(min⁡)=2k+1⇒k=1D(\min) = 2k + 1 \Rightarrow k = 1 error.
Exercise 2

A code uses 2 data bits plus 1 even parity bit. (a) List the legal code words and find D(min⁡)D(\min). (b) The words 010, 100 and 000 are received. For each, say whether an error is detected and whether it can be corrected.

Show solution ↓
Solution

(a) Data 00, 01, 10, 11 with an even-parity bit appended: 000, 011, 101, 110. Every pair differs in exactly 2 bits (e.g. 011 vs 101 → XOR 110 → 2), so D(min⁡)=2D(\min) = 2. That detects k=2−1=1k = 2 - 1 = 1 error and corrects ⌊(2−1)/2⌋=0\lfloor (2-1)/2 \rfloor = 0 errors.

(b)

ReceivedNumber of 1sDetected?Nearest legal words (distance 1)Correctable?
0101 (odd)yes000, 011, 110no: three equally close candidates
1001 (odd)yes000, 101, 110no
0000 (even)no(it is legal)accepted as 000

All 8 patterns 000–111 are either legal (4) or detectably illegal (4). A received 000 is accepted, though it would be wrong if a 2-bit error had turned 011 (or 101, 110) into 000; a distance-2 code cannot see that.

Exercise 3

How many check bits are needed to correct single-bit errors for data words of (a) 8 bits, (b) 16 bits, (c) 32 bits? Give the code-word length each time.

Show solution ↓
Solution

Use (m+r+1)≤2r(m + r + 1) \le 2^r and try increasing rr.

(a) m=8m = 8

  • r=3r = 3: 8+3+1=12≤88 + 3 + 1 = 12 \le 8? No.
  • r=4r = 4: 8+4+1=13≤168 + 4 + 1 = 13 \le 16? Yes.

r=4r = 4, n=12n = 12.

(b) m=16m = 16

  • r=4r = 4: 21≤1621 \le 16? No.
  • r=5r = 5: 22≤3222 \le 32? Yes.

r=5r = 5, n=21n = 21.

(c) m=32m = 32

  • r=5r = 5: 38≤3238 \le 32? No.
  • r=6r = 6: 39≤6439 \le 64? Yes.

r=6r = 6, n=38n = 38.

Notice the overhead shrinks as words get longer: 4/8=50%4/8 = 50\%, 5/16≈31%5/16 \approx 31\%, 6/32≈19%6/32 \approx 19\%.

Exercise 4

(Lecture example) Using even parity and the lecture’s layout, find the check bits (key KK) and the full 12-bit code word for the data word MM = 11010110.

Show solution ↓
Solution

Step 1: number of check bits. m=8⇒r=4m = 8 \Rightarrow r = 4, n=12n = 12 (Example 3).

Step 2: place the data bits in the non-power-of-2 positions from the top down: positions 12, 11, 10, 9, 7, 6, 5, 3 receive 1, 1, 0, 1, 0, 1, 1, 0.

position : 12 11 10  9  8  7  6  5  4  3  2  1
contents :  1  1  0  1 P8  0  1  1 P4  0 P2 P1

Step 3: compute each check bit (XOR of the data bits it covers):

Check bitCovers data positionsBits thereXOR
P13, 5, 7, 9, 110, 1, 0, 1, 11
P23, 6, 7, 10, 110, 1, 0, 0, 10
P45, 6, 7, 121, 1, 0, 11
P89, 10, 11, 121, 0, 1, 11

Key KK = P8 P4 P2 P1 = 1101.

Step 4: the code word (positions 12 → 1):

position : 12 11 10  9  8  7  6  5  4  3  2  1
bit      :  1  1  0  1  1  0  1  1  1  0  0  1

Code word = 110110111001.

Check: each group has an even number of 1s. Group 1 = positions 1, 3, 5, 7, 9, 11 = 1, 0, 1, 0, 1, 1 → four 1s ✓; group 2 = 2, 3, 6, 7, 10, 11 = 0, 0, 1, 0, 0, 1 → two ✓; group 4 = 4, 5, 6, 7, 12 = 1, 1, 1, 0, 1 → four ✓; group 8 = 8, 9, 10, 11, 12 = 1, 1, 0, 1, 1 → four ✓.

Exercise 5

(Lecture example, continued) The data MM = 11010110 was stored with key KK = 1101. It is read back from memory as MM = 11010100 (the stored key is still 1101). Detect and correct the error.

Show solution ↓
Solution

Step 1: place the data read out. Positions 12, 11, 10, 9, 7, 6, 5, 3 = 1, 1, 0, 1, 0, 1, 0, 0.

Step 2: recompute the check bits from this data:

Check bitCoversBits readNew value
P13, 5, 7, 9, 110, 0, 0, 1, 10
P23, 6, 7, 10, 110, 1, 0, 0, 10
P45, 6, 7, 120, 1, 0, 10
P89, 10, 11, 121, 0, 1, 11

New key K′K' = P8 P4 P2 P1 = 1000. (With these check bits the read-back word would be 110110100000.)

Step 3: compare with the stored key by XOR:

  stored K   1 1 0 1
⊕ new K'     1 0 0 0
  ---------------
  syndrome   0 1 0 1   = 5

P1 and P4 disagree; 4+1=54 + 1 = 5, which is not a power of 2, so data bit 5 is wrong.

Step 4: correct. Position 5 holds the 7th data bit (positions 12, 11, 10, 9, 7, 6, 5, 3). Flip it: 0 → 1.

Corrected MM = 11010110 ✓, the original data.

(Same answer by the full-group method on the stored word 110110101001: group 1 has bits 1, 0, 0, 0, 1, 1 → odd → 1; group 2 → 0, 0, 1, 0, 0, 1 → even → 0; group 4 → 1, 0, 1, 0, 1 → odd → 1; group 8 → 1, 1, 0, 1, 1 → even → 0. Syndrome 0101 = 5 ✓.)

Exercise 6

(a) Find the 7-bit Hamming code word (even parity, lecture layout) for the 4-bit data 1011. (b) The code word is read back as 1110101. Find and correct the error.

Show solution ↓
Solution

(a) m=4m = 4: r=3r = 3 since 4+3+1=8≤234 + 3 + 1 = 8 \le 2^3 ✓; n=7n = 7. Data in positions 7, 6, 5, 3 = 1, 0, 1, 1. Check bits at 4, 2, 1:

Check bitCoversBitsValue
P13, 5, 71, 1, 11
P23, 6, 71, 0, 10
P45, 6, 71, 0, 10
position : 7  6  5  4  3  2  1
bit      : 1  0  1  0  1  0  1

Code word 1010101, key P4 P2 P1 = 001.

(b) Received 1110101: positions 7 → 1 = 1, 1, 1, 0, 1, 0, 1. Data read (7, 6, 5, 3) = 1, 1, 1, 1; stored key P4 P2 P1 = 0, 0, 1.

Recompute: P1 = bits 3, 5, 7 = 1⊕1⊕1=11 \oplus 1 \oplus 1 = 1; P2 = bits 3, 6, 7 = 1⊕1⊕1=11 \oplus 1 \oplus 1 = 1; P4 = bits 5, 6, 7 = 1⊕1⊕1=11 \oplus 1 \oplus 1 = 1. New key = 111.

Syndrome = 001⊕111=110=6001 \oplus 111 = 110 = 6 → position 6 is wrong. Flip it: 1110101 → 1010101, data 1011 ✓.

Exercise 7

The 12-bit code word 110110111001 (from Example 4) is read back as 110100111001. Find the error. Is the data damaged?

Show solution ↓
Solution

Compare positions: the received word, positions 12 → 1, is 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1. Use the full-group method (XOR every bit in each group, check bit included):

GroupPositionsBits receivedXOR
11, 3, 5, 7, 9, 111, 0, 1, 0, 1, 10
22, 3, 6, 7, 10, 110, 0, 1, 0, 0, 10
44, 5, 6, 7, 121, 1, 1, 0, 10
88, 9, 10, 11, 120, 1, 0, 1, 11

Syndrome (8 4 2 1) = 1000 = 8, a power of 2: the error is in check bit P8 itself. The data bits (positions 12, 11, 10, 9, 7, 6, 5, 3 = 1, 1, 0, 1, 0, 1, 1, 0 = 11010110) are undamaged; we simply rewrite P8 = 1 to restore 110110111001.

(With the key method: data read 11010110 gives recomputed key 1101; stored key read as 0101; 1101⊕0101=1000=81101 \oplus 0101 = 1000 = 8 ✓.)