Sub topic 4
Hamming code: locating and correcting a single-bit error
This chapter assumes you know bitwise XOR, even parity, and the idea of Hamming distance from the previous chapter. A parity bit tells you that a frame is damaged but not where. A Hamming code adds several parity bits, each covering a different subset of positions, so the pattern of failed checks names the damaged position. The receiver can then flip that bit back without asking for a retransmission.
How many check bits are needed
Suppose a codeword has m data bits and r check bits, so its length is n = m + r. The receiver must distinguish n + 1 outcomes: no error, or an error in one of the n positions. r check bits can produce 2^r different syndromes, so:
2^r >= m + r + 1| Data bits m | Smallest r | Codeword length n | Name |
|---|---|---|---|
| 4 | 3 | 7 | Hamming(7,4) |
| 8 | 4 | 12 | |
| 11 | 4 | 15 | Hamming(15,11) |
| 12 | 5 | 17 |
Codes with m = 2^r - r - 1, such as (7,4) and (15,11), are called perfect. Every syndrome value is used.
Where the check bits go
Number the positions from 1, starting at the left. The check bits sit at the power-of-two positions 1, 2, 4, 8, and so on. All other positions hold data.
Check bit at position p covers every position whose binary representation has the bit p set. For 7 positions:
| Check bit | Positions covered |
|---|---|
| p1 | 1, 3, 5, 7 |
| p2 | 2, 3, 6, 7 |
| p4 | 4, 5, 6, 7 |
Each position belongs to a unique combination of check bits. Position 6 is binary 110, so it is covered by p4 and p2 only. That uniqueness is what makes locating the error possible.
Worked example with even parity
Encode the data 1011. The layout is p1 p2 d1 p4 d2 d3 d4, so positions 3, 5, 6, 7 hold 1, 0, 1, 1.
- p1 covers positions 3, 5, 7, which hold 1, 0, 1. That is two ones, so p1 = 0.
- p2 covers positions 3, 6, 7, which hold 1, 1, 1. That is three ones, so p2 = 1.
- p4 covers positions 5, 6, 7, which hold 0, 1, 1. That is two ones, so p4 = 0.
The codeword is 0110011.
Now suppose position 6 flips in transit, and the receiver gets 0110001. It recomputes each check over all positions in its group, including the check bit itself:
- Group 1 (positions 1, 3, 5, 7) holds 0, 1, 0, 1. The parity is even, so c1 = 0.
- Group 2 (positions 2, 3, 6, 7) holds 1, 1, 0, 1. The parity is odd, so c2 = 1.
- Group 4 (positions 4, 5, 6, 7) holds 0, 0, 0, 1. The parity is odd, so c4 = 1.
Reading the results as a binary number gives the syndrome c4 c2 c1 = 110 = 6. The error is in position 6. The receiver flips it and recovers 0110011.
A shortcut: the syndrome is an XOR of positions
With even parity, the syndrome equals the XOR of the position numbers of all 1 bits in the received word. For 0110001 the ones are at positions 2, 3 and 7, and 2 ^ 3 ^ 7 = 6. This is the fastest way to decode by hand. A valid codeword always gives 0, because the check bits are chosen to make that XOR vanish.
Implementation
def hamming_encode(data: str) -> str:
m = len(data)
r = 0
while 2 ** r < m + r + 1:
r += 1
n = m + r
code = [0] * (n + 1) # index 0 unused; positions are 1..n
bits = iter(data)
for pos in range(1, n + 1):
if pos & (pos - 1): # not a power of two, so a data position
code[pos] = int(next(bits))
for i in range(r):
p = 1 << i
code[p] = sum(code[pos] for pos in range(1, n + 1) if pos & p) % 2
return "".join(str(b) for b in code[1:])
def syndrome(word: str) -> int:
s = 0
for pos, bit in enumerate(word, start=1):
if bit == "1":
s ^= pos
return s
def correct(word: str):
s = syndrome(word)
if s == 0:
return word, 0
if s > len(word):
return None, s # cannot be a single-bit error
bits = list(word)
bits[s - 1] = "1" if bits[s - 1] == "0" else "0"
return "".join(bits), s
def flip(word: str, pos: int) -> str:
bits = list(word)
bits[pos - 1] = "1" if bits[pos - 1] == "0" else "0"
return "".join(bits)
sent = hamming_encode("1011")
print(sent) # 0110011
fixed, s = correct(flip(sent, 6))
print(s, fixed == sent) # 6 True
fixed, s = correct(flip(flip(sent, 3), 5))
print(s, fixed == sent) # 6 FalseThe last call shows the limit of the code. Two flipped bits produce a syndrome of 3 ^ 5 = 6, which looks like a single error at position 6. The decoder "corrects" the wrong bit and delivers corrupted data without any warning.
Properties and limits
- A Hamming code has minimum Hamming distance 3. Using the rule from the earlier chapter, it can detect 2 errors or correct 1, but not both at once.
- If a check bit is the one that flips, the syndrome is a power of two (1, 2, 4, ...). The decoder flips that check bit, and the data is untouched.
- Adding one overall parity bit over the whole codeword raises the distance to 4. This extended Hamming code corrects 1 error and detects 2, which is called SECDED. If the syndrome is nonzero but the overall parity is even, two errors occurred and the frame is rejected. Memory (ECC RAM) commonly uses this scheme.
- Burst errors defeat a plain Hamming code. Link-layer protocols rely on CRC for detection and use Hamming-style correction mainly where retransmission is costly or impossible.
Common mistakes
- Numbering positions from 0 or from the right. The standard layout counts from 1 at the left, and the syndrome is read as a position in that numbering.
- Computing
rfrom2^r >= m + rand forgetting the+ 1for the no-error case. Form = 4this givesr = 2, which is wrong. - Using odd parity for the check bits but an even-parity decoder. Fix the convention first. With odd parity, a valid word gives all groups odd, not a syndrome of 0 by the XOR shortcut.
- Leaving out the check bit itself when a receiver recomputes its group. The group parity includes it.
Exercise
How many check bits does a Hamming code need for 12 data bits, and what is the codeword length?
Show answer
Find the smallest r with 2^r >= 12 + r + 1. For r = 4, 16 < 17, so it fails. For r = 5, 32 >= 18, so r = 5. The codeword length is 12 + 5 = 17 bits.
Exercise
A receiver using even parity gets the 7-bit Hamming word 1001101 (positions numbered 1 to 7 from the left). Assume at most one bit is wrong. Find the error position and the corrected word.
Show answer
The ones are at positions 1, 4, 5 and 7. The XOR is 1 ^ 4 ^ 5 ^ 7 = 7, so the syndrome is 7 and position 7 is wrong. Flipping it gives 1001100. Check: the ones are now at 1, 4, 5, and 1 ^ 4 ^ 5 = 0.
Exercise
A Hamming(7,4) codeword suffers two bit errors at positions 3 and 5. What syndrome does the receiver compute, and what does it do?
Show answer
The syndrome is 3 ^ 5 = 6. The receiver assumes a single error at position 6 and flips it. The word then has three wrong bits, and the receiver does not notice. A distance-3 code cannot reliably handle two errors.