Sub topic 3
Hamming Distance: How Many Errors a Code Detects and Corrects
Assume you know binary numbers, bitwise XOR, and the role of the data link layer. This chapter uses a codeword, which is a valid bit pattern produced by an encoding scheme. A receiver compares a received bit pattern with valid codewords to find transmission errors.
Measuring the Distance Between Codewords
The Hamming distance between two equal-length bit strings is the number of positions in which they differ. You can calculate it by XORing the strings and counting the 1s.
For example:
x = 101101
y = 100001
x XOR y = 001100The XOR result contains two 1s, so the Hamming distance is 2.
A code contains several valid codewords. The minimum Hamming distance, written as d_min, is the smallest distance between any two different valid codewords.
Detection and Correction Rules
The minimum distance determines how many bit errors a code can handle.
| Minimum distance | Guaranteed detection | Guaranteed correction |
|---|---|---|
| `d_min` | `d_min - 1` errors | `floor((d_min - 1) / 2)` errors |
| 3 | 2 errors | 1 error |
| 5 | 4 errors | 2 errors |
To detect an error, the received pattern must become an invalid codeword. If up to d_min - 1 bits change, the result cannot equal another valid codeword.
To correct an error, the receiver chooses the nearest valid codeword. This works only when the received pattern is closer to the original codeword than to every other codeword. A code with d_min = 3 can correct one error because the correction regions around codewords do not overlap.
For example, suppose the valid codewords are 00000 and 11100. Their distance is 3. If 00000 becomes 00100, the receiver sees a pattern one bit from 00000 and two bits from 11100, so it corrects the received pattern to 00000.
However, two errors can produce a pattern that is equally close to different codewords or that is incorrectly interpreted. Therefore, a code designed only for single-bit correction should not silently correct patterns containing more errors.
Key Distinction
Detection asks, “Is this pattern valid?” Correction asks, “Which valid pattern was probably sent?” Correction requires a larger distance because the receiver must distinguish among competing codewords.
Exercise
A block code has d_min = 7. How many errors can it always detect and how many can it always correct?
Show answer
It can always detect 6 errors and correct 3 errors, because detection is d_min - 1 and correction is floor((d_min - 1) / 2).