Hamming Distance and Hamming Codes
1. Definition of Hamming Distance
The Hamming Distance between two words (binary sequences of equal length) and , denoted as , is the number of bit positions in which the corresponding bits differ.
It is mathematically calculated by performing a bitwise Exclusive-OR (XOR) operation between the two words and counting the number of 1s in the result (the Hamming Weight ):
x: 1 0 1 1 0 1
y: 1 0 0 1 1 1
--------------------------
x ⊕ y: 0 0 1 0 1 0 ===> Count of 1s = 2
Hamming Distance d(x, y) = 2
Minimum Hamming Distance ():
The Minimum Hamming Distance of a code is the smallest Hamming distance between any pair of distinct valid codewords in the entire codebook:
(For linear block codes, equals the minimum weight among all non-zero valid codewords).
2. Detection and Correction Capabilities
The error detection and correction capabilities of any block code are strictly bounded by its minimum Hamming distance :
Error Detection (d_min >= s + 1)
Valid C1 Valid C2
O ------------------ Distance d_min ----------------- O
|<--- s errors --->|
(Corrupted word falls strictly into the invalid gap between codewords)
Error Correction (d_min >= 2t + 1)
Codeword 1 Invalid Zone Codeword 2
( O ) | | ( O )
\__Radius t_/ \_Radius t__/
(Spheres of radius t around valid codewords must NOT overlap)
A. Error Detection Condition
To detect up to bit errors, the minimum Hamming distance must satisfy:
- Example: To detect all single-bit errors (), we need (achieved by simple parity check).
- To detect all 2-bit errors (), we need .
B. Error Correction Condition
To correct up to bit errors, the minimum Hamming distance must satisfy:
- Example: To correct any single-bit error ():
- If a single bit flips, the received word is at distance 1 from the original valid codeword and at least distance 2 from any other valid codeword, enabling unambiguous nearest-neighbor decoding.
3. The Hamming Code (Hamming Rule & Construction)
Richard Hamming designed an elegant single-error-correcting linear code.
Redundancy Bit Formula:
If an information message contains data bits and requires redundant parity bits, the total codeword length is . The parity bits must distinguish between single-bit error locations and 1 error-free state ( total states):
| Data Bits () | Redundant Bits () | Total Codeword Length () | Example Code |
|---|---|---|---|
| 1 | 2 | 3 | (3, 1) Triple Repetition |
| 4 | 3 | 7 | (7, 4) Hamming Code |
| 11 | 4 | 15 | (15, 11) Hamming Code |
| 26 | 5 | 31 | (31, 26) Hamming Code |
4. Construction of the Hamming (7, 4) Code
A (7, 4) Hamming code carries data bits () and parity bits ().
Bit Position Mapping:
Parity bits are placed strictly in bit positions that are powers of 2 (). All other positions hold data bits:
| Bit Position | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
|---|---|---|---|---|---|---|---|
| Binary Index | 111 |
110 |
101 |
100 |
011 |
010 |
001 |
| Role |
Parity Check Bit Calculation (Even Parity):
Each parity bit checks all bit positions whose binary representation has a 1 in the parity bit's position:
- (checks positions with LSB
1: 1, 3, 5, 7):
- (checks positions with 2nd bit
1: 2, 3, 6, 7):
- (checks positions with 3rd bit
1: 4, 5, 6, 7):
5. Solved Step-by-Step Example
Step 1: Encoding at Sender
Let the 4-bit dataword be 1 0 1 1 ().
Step 2: Error Injection during Transmission
Suppose bit 5 flips during transit ( changes from 1 to 0):
Step 3: Syndrome Calculation at Receiver
Compute syndrome bits using even parity check:
The syndrome decimal value identifies Bit 5 as the erroneous bit! The receiver flips bit 5 back from 0 to 1, completely correcting the error.
6. Key Exam Questions & Takeaways
- State the relationship between and error detection/correction.
- Detection: .
- Correction: .
- If a code has , what are its maximum error detection and correction capabilities?
- Max detection: bit errors.
- Max correction: bit errors.
- What is the Hamming inequality for calculating parity bits?
- , where is the number of data bits and is the number of parity bits.
- How does the syndrome locate the error in a Hamming code?
- When parity check equations are evaluated, the resulting syndrome binary bits directly form the 1-based decimal index of the corrupted bit. If syndrome is 0, no error occurred.