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 word
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.
The minimum Hamming distanceD(min) of a code is the smallest Hamming distance between all pairs of legal code words. It determines what the code can do:
Goal
Required minimum distance
detect up to k single-bit errors
D(min)=k+1
correct up to k single-bit errors
D(min)=2k+1
Why? Think of legal code words as islands.
If they are at least k+1 apart, then k flips can never turn one legal word into another, so the receiver always sees an illegal word: detected.
If they are at least 2k+1 apart, then a word with up to k flips is still closer to the original than to any other legal word (the other one is at least k+1 away), so the receiver corrects it to the nearest legal code word.
Turning the rules around: a code with distance D(min) detects D(min)−1 errors and corrects ⌊(D(min)−1)/2⌋ 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)=2 → it detects k=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
Take any legal code word of n bits. An error can hit any one of its n bits, giving n 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+1 patterns: itself plus its n one-bit-error neighbours. To correct single-bit errors these groups must not overlap. There are 2m legal words and only 2n patterns in total:
(n+1)2m≤2n⟹(m+r+1)2m≤2m+r⟹(m+r+1)≤2r
This gives the lower limit on the number of check bits. Find the smallest r that satisfies it:
m (data bits)
smallest r
check
n=m+r
4
3
4+3+1=8≤8
7
8
4
8+4+1=13≤16 (r=3: 12≤8 fails)
12
16
5
16+5+1=22≤32
21
32
6
32+6+1=39≤64
38
64
7
64+7+1=72≤128
71
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
Number the bit positions 1 to n from the right.
Put the check bits at the positions that are powers of 2: 1, 2, 4, 8, … .
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=8, n=12:
position : 12 11 10 9 8 7 6 5 4 3 2 1binary : 1100 1011 1010 1001 1000 0111 0110 0101 0100 0011 0010 0001contents : 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 c covers every position whose binary number has the c bit set.
Check bit
Binary
Covers positions
P1
0001
1, 3, 5, 7, 9, 11 (all odd positions)
P2
0010
2, 3, 6, 7, 10, 11
P4
0100
4, 5, 6, 7, 12
P8
1000
8, 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+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=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.
Place the data bits.
For each check bit c: XOR the data bits in the positions it covers.
Write the check bits in place. The keyK is the check bits read P8 P4 P2 P1.
The error-correcting function in memory works like this: when a word is written, a function f computes the key K from the data M and both are stored. When the word is read, f is applied again to the data read out, and the new key is compared with the stored key:
Diagram loads as you scroll.
Procedure
Recompute the check bits from the data bits that were read: K′.
XOR with the stored key: syndrome=K⊕K′ (read as a binary number P8 P4 P2 P1).
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 c, 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)=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) of the code {00000,01011,10101,11110}. How many errors can it detect and how many can it correct?
Show solution ↓Hide solution ↑
Solution
(a) XOR and count the 1s:
10001001⊕ 10110001 -------- 00111000 → 3 ones
Hamming distance 3.
(b) There are (24)=6 pairs:
Pair
XOR
Distance
00000, 01011
01011
3
00000, 10101
10101
3
00000, 11110
11110
4
01011, 10101
11110
4
01011, 11110
10101
3
10101, 11110
01011
3
D(min)=3.
Detect: D(min)=k+1⇒k=2 errors.
Correct: D(min)=2k+1⇒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).
(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 ↓Hide 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)=2. That detects k=2−1=1 error and corrects ⌊(2−1)/2⌋=0 errors.
(b)
Received
Number of 1s
Detected?
Nearest legal words (distance 1)
Correctable?
010
1 (odd)
yes
000, 011, 110
no: three equally close candidates
100
1 (odd)
yes
000, 101, 110
no
000
0 (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 ↓Hide solution ↑
Solution
Use (m+r+1)≤2r and try increasing r.
(a) m=8
r=3: 8+3+1=12≤8? No.
r=4: 8+4+1=13≤16? Yes.
r=4, n=12.
(b) m=16
r=4: 21≤16? No.
r=5: 22≤32? Yes.
r=5, n=21.
(c) m=32
r=5: 38≤32? No.
r=6: 39≤64? Yes.
r=6, n=38.
Notice the overhead shrinks as words get longer: 4/8=50%, 5/16≈31%, 6/32≈19%.
Exercise 4
(Lecture example) Using even parity and the lecture’s layout, find the check bits (key K) and the full 12-bit code word for the data word M = 11010110.
Show solution ↓Hide solution ↑
Solution
Step 1: number of check bits.m=8⇒r=4, n=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.
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 M = 11010110 was stored with key K = 1101. It is read back from memory as M = 11010100 (the stored key is still 1101). Detect and correct the error.
Show solution ↓Hide 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 bit
Covers
Bits read
New value
P1
3, 5, 7, 9, 11
0, 0, 0, 1, 1
0
P2
3, 6, 7, 10, 11
0, 1, 0, 0, 1
0
P4
5, 6, 7, 12
0, 1, 0, 1
0
P8
9, 10, 11, 12
1, 0, 1, 1
1
New key 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=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 M = 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 ↓Hide solution ↑
Solution
(a)m=4: r=3 since 4+3+1=8≤23 ✓; n=7. Data in positions 7, 6, 5, 3 = 1, 0, 1, 1. Check bits at 4, 2, 1:
Syndrome = 001⊕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 ↓Hide 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):
Group
Positions
Bits received
XOR
1
1, 3, 5, 7, 9, 11
1, 0, 1, 0, 1, 1
0
2
2, 3, 6, 7, 10, 11
0, 0, 1, 0, 0, 1
0
4
4, 5, 6, 7, 12
1, 1, 1, 0, 1
0
8
8, 9, 10, 11, 12
0, 1, 0, 1, 1
1
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=8 ✓.)