Parity and Hamming codes
About this lesson
Noise flips bits. A single parity bit can tell you that a block went wrong, but not where. Richard Hamming's 1950 code adds three parity bits over overlapping groups, so the checks that fail spell out the position of the bad bit. Follow one worked example: the data 1011 becomes the codeword 0110011, slot 5 flips on the way, the receiver's checks give the syndrome 101 = 5, and flipping that bit back recovers 1011. You will also see what the code buys, where it breaks, and how numpy does the checks as one matrix product. It follows the Shannon lesson on channel capacity.
Noise flips a bit
Send four data bits, 1011, over a noisy link. Noise (random voltage added on the way) can push a bit past the receiver's cut-off between 0 and 1, so a 0 arrives as a 1. The receiver gets 1111 and cannot tell.
sent 1011 noise flips one bit (slot 5 here) received 1111 1111 is also a valid message, so nothing looks wrong. why bits flip: the AWGN lesson (noise + picking the nearest level)
Read the lesson step by step
Noise flips a bit
Send four data bits, 1011, over a noisy link. Noise (random voltage added on the way) can push a bit past the receiver's cut-off between 0 and 1, so a 0 arrives as a 1. The receiver gets 1111 and cannot tell.
sent 1011 noise flips one bit (slot 5 here) received 1111 1111 is also a valid message, so nothing looks wrong. why bits flip: the AWGN lesson (noise + picking the nearest level)One parity bit catches it
Add one parity bit P so the count of 1s is even: 1011 has 3 ones, so P = 1. One flip makes the count odd, and the receiver knows something broke. It cannot tell which bit, and two flips cancel out.
even parity: P = d1 ⊕ d2 ⊕ d3 ⊕ d4 ⊕ = XOR: 1 if an odd number of 1s 1 ⊕ 0 ⊕ 1 ⊕ 1 = 1 sent 1011 1 (4 ones: even) received 1111 1 (5 ones: odd) detects 1, 3, 5 ... flips misses 2, 4 ... flips never says WHICH bitHamming's layout: seven slots
Hamming's idea (1950): use three parity bits, each over a different, overlapping group. Number seven slots 1 to 7. The parity bits go in slots 1, 2 and 4 (powers of two); the data 1011 fills slots 3, 5, 6 and 7.
slot 1 2 3 4 5 6 7 role p1 p2 d1 p4 d2 d3 d4 bit ? ? 1 ? 0 1 1 slot in binary (4 2 1): 1 = 001 3 = 011 5 = 101 7 = 111 2 = 010 6 = 110 4 = 100 group k = the slots whose number has bit k setp1 checks slots 1, 3, 5, 7
Parity bit p1 makes its group even. Its group is every odd slot: 1, 3, 5 and 7. The data there is 1, 0 and 1: two ones, already even, so p1 = 0.
group 1 = slots 1 3 5 7 (slot number ends in binary 1) p1 = slot3 ⊕ slot5 ⊕ slot7 = 1 ⊕ 0 ⊕ 1 = 0 word so far: 0 ? 1 ? 0 1 1p2 checks slots 2, 3, 6, 7
p2 covers slots 2, 3, 6 and 7 (their numbers have the 2s bit set). The data there is 1, 1 and 1: three ones, an odd count, so p2 = 1 to make it even.
group 2 = slots 2 3 6 7 (binary 010 011 110 111) p2 = slot3 ⊕ slot6 ⊕ slot7 = 1 ⊕ 1 ⊕ 1 = 1 word so far: 0 1 1 ? 0 1 1p4 checks slots 4, 5, 6, 7
p4 covers slots 4 to 7. The data there is 0, 1 and 1: even, so p4 = 0. The codeword is 0110011. Seven bits now carry four: the code rate is 4/7 ≈ 0.571.
group 4 = slots 4 5 6 7 p4 = slot5 ⊕ slot6 ⊕ slot7 = 0 ⊕ 1 ⊕ 1 = 0 codeword 0110011 p1 p2 d1 p4 d2 d3 d4 rate = data bits / sent bits = 4 / 7 = 0.571The codeword crosses the noise
All seven bits go out. Noise flips slot 5 again, a data bit this time hidden among parity bits. The receiver holds 0110111, one bit away from what was sent.
sent 0110011 flipped slot 5 received 0110111 The receiver does not know the sent word. It only knows the three group rules.The failed checks name the slot
The receiver recounts each group. Group 1 is odd: fail. Group 2 is even: pass. Group 4 is odd: fail. Add the failing group numbers: 1 + 4 = 5. That pattern, 101 in binary, is called the syndrome.
received 0110111 check 1: slots 1 3 5 7 = 0 1 1 1 -> 1 (odd) check 2: slots 2 3 6 7 = 1 1 1 1 -> 0 (even) check 4: slots 4 5 6 7 = 0 1 1 1 -> 1 (odd) syndrome c4 c2 c1 = 101 = 5 only slot 5 is in groups 1 and 4 but not in group 2 syndrome 000 = no error seenFlip it back and read the data
Flip slot 5 back: 0110111 becomes 0110011, the word that was sent. Read the data from slots 3, 5, 6 and 7: 1011. The error was found and fixed with no resend.
received 0110111 flip slot 5 ↓ corrected 0110011 data = slots 3 5 6 7 = 1011 ✓ every single flip, in any of the 7 slots, gives its own syndrome (001 .. 111), so any ONE error is found and fixedWhat it buys, and its limit
If each bit flips 1 time in 100, four bare bits arrive wrong 3.9% of the time; Hamming(7,4) fails only when two or more of its seven flip: 0.20%. Two flips fool it: it fixes the wrong slot.
p = chance one sent bit flips bare 4 bits: 1 − (1−p)⁴ Hamming(7,4): 1 − (1−p)⁷ − 7p(1−p)⁶ p = 0.01: 3.9% vs 0.20% any two codewords differ in ≥ 3 bits (minimum distance 3): 1 flip -> corrected 2 flips -> nearer to a wrong word an 8th overall parity bit (SECDED) detects the double: ECC memoryThe checks are a matrix product
In Python with numpy, each row of a matrix H marks one group. H @ r % 2 counts each group's ones and keeps odd or even: the syndrome. Wi-Fi 6 and 5G use far longer low-density parity-check (LDPC) codes built on this idea.
import numpy as np H = np.array([[1,0,1,0,1,0,1], [0,1,1,0,0,1,1], [0,0,0,1,1,1,1]]) r = np.array([0,1,1,0,1,1,1]) s = H @ r % 2 >>> s array([1, 0, 1]) >>> int(s @ [1, 2, 4]) 5 row 1 of H = group 1 (slots 1 3 5 7) (Python 3 with numpy) Next: CRC, for bursts of errors
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01A block is sent with one even-parity bit. Two of its bits flip on the way. What does the receiver see?
Each flip changes the count of 1s by one. Two flips change it twice, so it is even again and the check passes. A single parity bit detects only an odd number of flips, and never says which bit.
02In Hamming(7,4), which slots does parity bit p2 cover?
p2 covers every slot whose number has the 2s bit set in binary: 2 (010), 3 (011), 6 (110) and 7 (111).
03The receiver gets a word whose checks give c1 = 0, c2 = 1, c4 = 1. Which slot is wrong?
Add the numbers of the failing checks: 2 + 4 = 6. Read as binary c4 c2 c1 = 110, also 6. Only slot 6 is in groups 2 and 4 but not in group 1.
04Hamming(7,4) sends 7 bits for every 4 data bits. What is its code rate?
Code rate = data bits / sent bits = 4/7 ≈ 0.571. The other 3/7 of the bits are redundancy that buys error correction.
05Each bit flips on its own with chance 1 in 100. About how often does a Hamming(7,4) block decode wrongly?
It fails only when two or more of the 7 bits flip: 1 − (1−p)⁷ − 7p(1−p)⁶ ≈ 0.20% at p = 0.01. Four bare bits would fail 3.9% of the time.
Glossary
- Bit error
- A bit that arrives with the wrong value, usually because noise pushed the received signal past the cut-off level the receiver uses to choose 0 or 1.
- Parity bit
- An extra bit chosen so that the count of 1s in a group is even (even parity). It detects any odd number of flips but cannot say which bit flipped.
- XOR (⊕)
- Exclusive OR: 1 when an odd number of its inputs are 1. The parity of a group is the XOR of its bits.
- Codeword
- The full block that is sent: data bits plus parity bits. Hamming(7,4) turns 4 data bits into a 7-bit codeword.
- Code rate
- Data bits divided by sent bits; 4/7 for Hamming(7,4).
- Syndrome
- The pattern of failed parity checks at the receiver. In Hamming(7,4) it is the binary number of the flipped slot, or 000 if no error is seen.
- Hamming distance
- The number of positions in which two words differ. Any two Hamming(7,4) codewords differ in at least 3 bits (minimum distance 3).
- SECDED
- Single error correction, double error detection: a Hamming code plus one overall parity bit, as used in most ECC memory.
Notes and sources
- The data 1011, the flipped slot 5 and the flip chance 1 in 100 are chosen examples. Every bit, check and percentage in the figure is computed from this model.
- The percentages assume each bit flips on its own with the same chance p (a binary symmetric channel). Real links also suffer bursts of errors in a row; the CRC lesson handles those.
- Richard Hamming published these codes at Bell Labs in 1950 (Bell System Technical Journal 29(2)). Most ECC memory uses a Hamming-type code with one extra parity bit (SECDED).
- R. W. Hamming, Error detecting and error correcting codes, Bell System Technical Journal 29(2):147-160, 1950 (IEEE Xplore record)
- Hamming(7,4) (Wikipedia): bit layout, parity coverage, 1011 encodes to 0110011, syndrome 101 points at bit 5
- Hamming code (Wikipedia): minimum distance 3, double errors miscorrected, extended code and SECDED
- Parity bit (Wikipedia): detects only an odd number of errors and cannot locate one
- MIT OpenCourseWare 6.02 Digital Communication Systems (H. Balakrishnan, G. Verghese, Fall 2012)
- MIT 6.02 notes, ch. 5: Coping with bit errors using error correction codes
- MIT 6.02 notes, ch. 6: Linear block codes: encoding and syndrome decoding
- S. Haykin, M. Moher, Communication Systems, 5th ed., Wiley, 2009
- ECC memory (Wikipedia): most ECC memory uses a SECDED Hamming-type code
- Low-density parity-check code (Wikipedia): LDPC in Wi-Fi 6 (802.11ax) and 5G NR data channels
- NumPy documentation: numpy.matmul (the @ operator)
Quiz Unlocks after the last step.