Sub topic 3
Hamming distance: how many errors a code detects and corrects
This chapter assumes you know binary numbers and bitwise XOR, and that you understand why a data link layer adds redundancy to frames. It also assumes you have seen single-bit and burst errors and simple parity checks. The goal is a precise rule for how many errors any block code can detect or correct, and the skill to apply that rule in GATE-style questions.
Codewords and the code
A block code maps each k-bit dataword to an n-bit codeword, with n > k. Only 2^k of the 2^n possible n-bit strings are valid codewords. The other strings are invalid, and a receiver that sees one knows an error occurred.
Take a 2-bit dataword and add an even parity bit. The code has four codewords: 000, 011, 101, 110. The other four 3-bit strings (001, 010, 100, 111) are invalid.
Hamming distance
The Hamming distance between two equal-length bit strings is the number of positions in which they differ. You compute it by XORing the strings and counting the 1s.
def hamming_distance(a: str, b: str) -> int:
assert len(a) == len(b)
return sum(x != y for x, y in zip(a, b))
def code_distance(codewords: list[str]) -> int:
best = None
for i in range(len(codewords)):
for j in range(i + 1, len(codewords)):
d = hamming_distance(codewords[i], codewords[j])
if best is None or d < best:
best = d
return best
print(hamming_distance("10110", "11100")) # 2
print(code_distance(["000", "011", "101", "110"])) # 2
print(code_distance(["00000", "01011", "10101", "11110"])) # 3The minimum distance dmin of a code is the smallest Hamming distance over all pairs of distinct codewords. It is the single number that determines the code's power. A code with dmin = 1 has two codewords that differ in one bit, so one bit error can turn one valid codeword into another and nothing can be detected.
Detection: d_min ≥ s + 1
An error of weight s flips s bits, so the received word is at distance s from the sent codeword. The receiver detects the error if the received word is not a valid codeword.
If dmin ≥ s + 1, no codeword is within distance s of any other codeword. Any error of up to s bits therefore lands on an invalid string, and the receiver detects it. If dmin = s, some error pattern of s bits turns one codeword into another, and that error goes undetected.
A code detects up to s errors if and only if d_min ≥ s + 1. Equivalently, it detects d_min − 1 errors.
Check with even parity: dmin = 2, so it detects 1 error. It also detects any odd number of errors, but it guarantees only 1. Two flipped bits can map one codeword onto another, which is why the guarantee stops at dmin − 1.
Correction: d_min ≥ 2t + 1
To correct errors, the receiver uses nearest-codeword decoding. It replaces the received word with the closest valid codeword. This is correct only if the sent codeword is strictly closer to the received word than any other codeword.
Draw a sphere of radius t around each codeword, containing all strings within t bit flips. If an error has weight at most t, the received word lies inside the sphere of the sent codeword. Decoding is unambiguous if the spheres do not overlap, which requires that two codewords be at least 2t + 1 apart.
A code corrects up to t errors if and only if d_min ≥ 2t + 1. Equivalently, it corrects ⌊(d_min − 1)/2⌋ errors.
With d_min = 3, a single error is corrected, because the received word is distance 1 from the sent codeword and distance 2 from the other. A double error from A lands at distance 1 from B, and the decoder wrongly picks B.
Detection and correction together
You can split the distance between correction and detection. A code can correct up to t errors and, in addition, detect up to s errors (s ≥ t) if:
d_min ≥ t + s + 1
The decoder corrects when the received word is within distance t of a codeword. Otherwise it reports an uncorrectable error. With d_min = 4, you can correct 1 and detect 2 (t = 1, s = 2), or detect 3 without correcting anything.
| d_min | Detect only | Correct only | Correct t and detect s |
|---|---|---|---|
| 1 | 0 | 0 | none |
| 2 | 1 | 0 | none |
| 3 | 2 | 1 | t=1, s=1 |
| 4 | 3 | 1 | t=1, s=2 |
| 5 | 4 | 2 | t=2, s=2 |
| 6 | 5 | 2 | t=2, s=3 |
Note that dmin = 3 gives either "detect 2" or "correct 1", but not both at once. The row shows that t = 1 with s = 1 fits, because 1 + 1 + 1 = 3. Correcting 1 error while still detecting 2 would need dmin ≥ 4.
Worked example
A code has the codewords 00000, 01011, 10101, 11110. The codedistance call above gives dmin = 3. Verify two pairs by hand:
- 00000 and 01011 differ in 3 positions.
- 10101 and 11110 differ in 3 positions (positions 2, 4, 5 counting from the left).
So this code detects 2 errors or corrects 1. Suppose the receiver gets 01001. The distances to the four codewords are 2, 1, 3, 4 (in the order listed above), so the nearest is 01011 and the decoder outputs it.
def decode(received: str, codewords: list[str]) -> str:
return min(codewords, key=lambda c: hamming_distance(received, c))
words = ["00000", "01011", "10101", "11110"]
print(decode("01001", words)) # 01011Common errors and edge cases
- Off-by-one in the formulas. Detection uses dmin − 1. Correction uses ⌊(dmin − 1)/2⌋. Students often write dmin/2 for correction, which is wrong for even dmin: d_min = 4 corrects 1, not 2.
- Guarantee versus possibility. A code with dmin = 3 detects some errors of weight 3 and above. Only the guarantee is dmin − 1. GATE questions ask for the guaranteed number.
- Confusing the distance of a code with that of a pair. The minimum is over all pairs. One close pair is enough to limit the whole code.
- Linear codes. For a linear code, the XOR of any two codewords is also a codeword, so d_min equals the smallest weight (number of 1s) among the nonzero codewords. This avoids checking every pair.
- Redundancy cost. Increasing dmin requires more check bits. The next chapter shows how a Hamming code reaches dmin = 3 with the fewest check bits.
Typical GATE patterns
- Given a set of codewords, find d_min and state the number of detectable and correctable errors.
- Given a requirement such as "detect 3 and correct 2", compute the needed d_min. Here t + s + 1 = 2 + 3 + 1 = 6.
- Decide whether a received word decodes uniquely to one codeword.
Exercise
A block code has minimum Hamming distance 7. (a) How many errors can it detect if used only for detection? (b) How many can it correct if used only for correction? (c) If it must correct 2 errors, how many errors can it additionally detect?
Show answer
(a) dmin − 1 = 6 errors. (b) ⌊(7 − 1)/2⌋ = 3 errors. (c) With t = 2, the condition dmin ≥ t + s + 1 gives 7 ≥ 2 + s + 1, so s ≤ 4. The code corrects 2 errors and detects up to 4 errors in total.
Exercise
The codewords of a linear code are 0000000, 1110100, 0111010, 1001110. What is d_min, and can the code correct a single-bit error?
Show answer
Since the code is linear, dmin is the smallest weight among the nonzero codewords. The weights are 4 (1110100), 4 (0111010) and 4 (1001110). So dmin = 4. It corrects ⌊(4 − 1)/2⌋ = 1 error, so yes, a single-bit error can be corrected. It can also detect 3 errors if used only for detection.