Hamming code (error correction)
Correcting errors without retransmission: Hamming distance and what the minimum distance of a code detects and corrects, the Hamming code with its check bits at the powers of two and the syndrome that names the wrong position, how many check bits m data bits need, the extended code that also detects double errors, and the odd-parity variant and the numbering conventions that questions use.
Why it exists
The CRC and the checksum detect that a frame was damaged; the cure is to send it again. That is expensive or impossible when the round trip is long (a space probe), when there is no return channel (broadcast, a CD), or when the data are stored (computer memory, where there is nobody to ask for a second copy). Error-correcting codes add enough redundancy that the receiver can find which bits are wrong and flip them back: forward error correction. The Hamming code is the classic and simplest one. It is not named in the official syllabus, but the idea of the distance of a code and the check-bit formulas appear in the data-link-layer questions of the exam.
Prerequisites
You need the idea of redundancy from error detection: parity bits, and a codeword that is a data word with extra bits.
Precise definitions
Hamming distance of two words of equal length: the number of positions in which they differ. For and it is 2.
Code with data bits and check bits: the set of the valid codewords of length out of all words. Minimum distance of the code: the smallest distance between two different codewords.
What the distance buys.
- It detects every error pattern of up to flipped bits (no such pattern can turn a codeword into another codeword).
- It corrects every pattern of up to flipped bits (the received word is nearer to the sent codeword than to any other).
- It corrects up to and at the same time detects up to errors if .
Hamming code. Number the positions from the right. The positions that are powers of two (1, 2, 4, 8, ...) hold the check bits ; all other positions hold the data bits in order. The check bit is chosen so that the group of positions whose binary index has bit set contains an even number of ones (even parity; with odd parity the number of ones must be odd). The groups for a 12-bit word:
| Check bit | Positions in its group |
|---|---|
| 1, 3, 5, 7, 9, 11 | |
| 2, 3, 6, 7, 10, 11 | |
| 4, 5, 6, 7, 12 | |
| 8, 9, 10, 11, 12 |
Syndrome. The receiver re-checks the groups; a group whose parity is wrong gives a 1. Read as a binary number (check of group 8 first), the result is the syndrome: 0 means every check passes, otherwise it is the position of a single wrong bit.
Number of check bits. A single error can be in any of the positions, or there is none: cases, each needing its own syndrome out of . So .
How it works
Encoding
Encoding 1011 with a (7,4) Hamming code: start
Computed by a script, not typed| check bit | Positions in its group | Data bits there | Ones among them | Value of the check bit |
|---|---|---|---|---|
| c1 | ||||
| c2 | ||||
| c4 | ||||
| codeword |
Step 0
Four data bits d4 d3 d2 d1 = 1 0 1 1. A Hamming code with r = 3 check bits has 7 positions numbered from the right; the check bits c1, c2, c4 sit at positions 1, 2, 4 (the powers of two), the data bits at positions 3, 5, 6, 7: d1 at 3, d2 at 5, d3 at 6, d4 at 7 (d1 = 1, d2 = 1, d3 = 0, d4 = 1). Check bit c(2^k) covers every position whose binary index has bit k set.
The data go to positions 7, 6, 5, 3; the check bits are computed one at a time. Each data bit takes part in the groups given by the ones of its position's binary index: position 3 = 011 is in the groups of and , position 7 = 111 in all three. That is why a single flipped bit is recognised by which groups it spoils.
Decoding: the syndrome is the position
Correcting a single-bit error with the syndrome: start
Computed by a script, not typed| step | Positions | Bits received there | Ones | Result |
|---|---|---|---|---|
| check 1 | ||||
| check 2 | ||||
| check 4 | ||||
| syndrome | ||||
| corrected word |
Step 0
The word 1010101 was sent and arrives as 1000101: position 5 was flipped. The receiver recomputes each group's parity.
Position 5 (101 in binary) is in the groups of and , so exactly those two checks fail, and the failed checks read (check 4 on the left). In general, flipping position spoils exactly the groups that contain , which are the groups of the one-bits of : the syndrome is the binary number itself. If the syndrome is 0 either there is no error or the pattern is invisible.
Two flipped bits at positions and give the syndrome , which is non-zero and is mistaken for a single error at that position: the decoder flips a third bit and delivers wrong data. The plain Hamming code corrects one error and cannot recognise two; its minimum distance is 3, so it detects two errors only if it is not asked to correct.
How many check bits?
How many check bits?: start
Computed by a script, not typed| data bits | Check bits r | Codeword length | Data bits / length | Check bits with double-error detection | Length with double-error detection |
|---|---|---|---|---|---|
| m = 1 | |||||
| m = 4 | |||||
| m = 8 | |||||
| m = 11 | |||||
| m = 16 | |||||
| m = 26 | |||||
| m = 32 | |||||
| m = 57 | |||||
| m = 64 | |||||
| m = 120 | |||||
| m = 128 |
Step 0
A single error can be in any of the n = m + r positions, or there is no error: n + 1 situations, each needing its own syndrome out of 2^r. So 2^r ≥ m + r + 1. The rows with 1, 4, 11, 26, 57 and 120 data bits (codeword length 2^r - 1) are the perfect codes.
The cost of the redundancy falls as the words get longer: 3 check bits for 4 data bits, but only 7 for 120. The codes of length (7, 15, 31, 63, 127) use the syndromes completely and are perfect: every word is either a codeword or one flip away from exactly one. In a computer memory, 64 data bits need 7 check bits for single-error correction and 8 for the extended code below (72 bits stored for 64 of data).
Distance, detection and correction
Minimum distance and what it buys: start
Computed by a script, not typed| code | Length n | Codewords | Minimum distance d | Detects | Corrects | Balls of radius 1 |
|---|---|---|---|---|---|---|
| every word of length 3 | ||||||
| single parity bit, length 4 (even-weight words) | ||||||
| repetition code 000, 111 | ||||||
| Hamming (7,4) | ||||||
| extended Hamming (8,4) | ||||||
| Hamming (12,8) |
Step 0
The Hamming distance of two words is the number of positions in which they differ. The minimum distance d of a code is the smallest distance between two different codewords. Every row was computed by comparing all pairs of codewords.
Each row was found by comparing all pairs of codewords and confirmed with all error patterns. Observe: an even-parity bit gives (detects one error, corrects none), the repetition code 000, 111 gives , and the Hamming code gives however long it is. The balls of radius 1 around the codewords of the Hamming code cover all words, so it is perfect; the extended code with is not (it has even, so a word can be at distance 2 from two codewords).
Detecting double errors as well: the extended code
Add one more parity bit over all the bits of the Hamming word. The minimum distance rises to 4, so the code corrects 1 error and detects 2 ().
Single-error correction, double-error detection: start
Computed by a script, not typed| case | Received word | Syndrome | Parity of all 8 bits | Decoder says | Outcome |
|---|---|---|---|---|---|
| no error | |||||
| one error (position 3) | |||||
| error in the overall parity bit | |||||
| two errors (positions 3 and 5) | |||||
| three errors (positions 1, 2, 4) |
Step 0
The data 1011 encoded with the extended code: the Hamming word 1010101 plus an overall even-parity bit in front: 0 1010101. The decoder uses the syndrome of the seven Hamming bits and the parity of all eight: syndrome zero and parity even means no error; parity odd means one error (at the syndrome's position, or in the overall bit if the syndrome is zero); syndrome non-zero and parity even means two errors.
The decoder looks at the syndrome of the Hamming bits and at the overall parity. Overall parity odd: one error (the syndrome gives its position; syndrome 0 means the overall bit itself). Overall parity even and syndrome not zero: two errors, detected but not corrected. With three errors the code can be fooled (the last row); the guarantee covers one and two only. This single-error-correcting, double-error-detecting scheme (SEC-DED) is the one used in error-correcting memory.
Conventions that change the answer
- Odd parity: each group must hold an odd number of ones. The check bits are the complement of the even-parity ones, and an all-zero data word does not give an all-zero codeword (for 1011 the word is 1011110 instead of 1010101). The syndrome still names the position.
- Position numbering: from the right (c1 at the far right), as above, or from the left. Read the question's layout; the rule "check bits at the powers of two, group = positions with that bit set in the index" holds in both, it only changes where the word is written.
- A word with an error in a check bit: the syndrome names that check bit's position; the correction flips it. The data bits were never wrong.
Worked examples
EX 1Positions and counts
EasyHow many check bits do 8 data bits need? , : 4. The codeword has 12 bits; the check bits are at positions 1, 2, 4 and 8; the layout from the right is . The check bit covers positions 8 to 12.
EX 2Encode and correct a 12-bit word
MediumData . Place them: at position 3, at 5, at 6, at 7, at 9, at 10, at 11, at 12. covers 3, 5, 7, 9, 11 with bits 0, 1, 0, 1, 0: two ones, so . covers 3, 6, 7, 10, 11: bits 0, 0, 0, 1, 0: one one, . covers 5, 6, 7, 12: bits 1, 0, 0, 1: two ones, . covers 9, 10, 11, 12: bits 1, 1, 0, 1: three ones, . The codeword, positions 12 to 1: 101110010010. If position 9 is flipped in transit, the received word is 101010010010: the checks of and fail (position 9 is in both), the syndrome is , flip position 9 and the codeword is back.
EX 3Decode a received word
MediumThe 12-bit word 011001101100 is received (layout as above). Write it out position by position, from 12 down to 1: position 12 = 0, 11 = 1, 10 = 1, 9 = 0, 8 = 0, 7 = 1, 6 = 1, 5 = 0, 4 = 1, 3 = 1, 2 = 0, 1 = 0. Now the four groups. Group (positions 1, 3, 5, 7, 9, 11) holds 0, 1, 0, 1, 0, 1: three ones, fails. Group (2, 3, 6, 7, 10, 11) holds 0, 1, 1, 1, 1, 1: five ones, fails. Group (4, 5, 6, 7, 12) holds 1, 0, 1, 1, 0: three ones, fails. Group (8, 9, 10, 11, 12) holds 0, 0, 1, 1, 0: two ones, passes. The syndrome, check 8 first, is . Flip position 7: the corrected word is 011000101100 and the data bits (positions 12, 11, 10, 9, 7, 6, 5, 3) are 01100101. Writing the word out position by position, as done here, avoids the most common mistake: reading the word from the wrong end.
EX 4Distance, detection, correction
GATE level(a) The code : the six pairwise distances are 3, 3, 3, 3, 4 and 4, so : it detects 2 errors and corrects 1. (b) "A code has . How many errors can it detect, and correct?" Detect 4, correct 2. Used together: correct 2 and detect 2 (), or correct 1 and detect 3 (). (c) "How many check bits for 16 data bits? For 57?" 5 (; ) and 6 (). (d) "The syndrome of a received Hamming word is 0110. What do you do?" Flip position 6 (assuming a single error). (e) "Two bits are flipped in a Hamming (7,4) word. What happens?" The decoder assumes one error and flips a third bit: wrong data, no warning.
Cheat box
Cheat box
- Hamming distance = number of differing positions. Code distance : detects , corrects ; corrects and detects if .
- Hamming code: check bits at positions 1, 2, 4, 8, ... Group of = positions whose index has bit set. Even parity: each group has an even number of ones.
- Syndrome = the failed checks read as a binary number = the position of the single wrong bit (0 = no error).
- Check bits: . For = 4, 8, 11, 16, 26, 57, 64: = 3, 4, 4, 5, 5, 6, 7.
- Plain Hamming: , corrects 1 and does not recognise 2. Extended (one more overall parity bit): , corrects 1 and detects 2 (SEC-DED).
Traps and GATE patterns
Connections
Across subjects
No cross-subject links yet.
Error detection and correction are two uses of the same redundancy, the distance between valid words: a code with distance detects errors, and uses the same space to correct fewer. Detection plus retransmission suits links with feedback (see the sliding-window protocols); correction suits links where retransmission is costly. The same SEC-DED codes protect main memory.
PYQs
2 previous year questions from your book. I re-solved each one myself (2 by running or simulating, 0 by reasoning only); the book's key is shown next to my answer. All PYQs
Assume that a 12-bit Hamming codeword consisting of 8-bit data and 4 check bits is d8d7d6d5c8d4d3d2c4d1c2c1, where the data bits and the check bits are given in the following tables: Data Bits d8 d7 d6 d5 d4 d3 d2 d1 1 1 0 x 0 1 0 1 Check Bits c8 c4 c2 c1 y 0 1 0 Which one of the following choices gives the correct values of x and y?
- (a)x is 0 and y is 0
- (b)x is 0 and y is 1
- (c)x is 1 and y is 0
- (d)x is 1 and y is 1
Consider a binary code that consists of only four valid codewords as given below: 00000, 01011, 10101, 11110. Let the minimum Hamming distance of the code be p and the maximum number of erroneous bits that can be corrected by the code be q. Then the values of p and q are:
- (a)p=3 and q=1
- (b)p=3 and q=2
- (c)p=4 and q=1
- (d)p=4 and q=2
Self-check
Question 1
A code has minimum distance 6. How many errors can it detect? How many can it correct?
Show answerHide
It detects 5 and corrects . (It can correct 2 and detect up to 3 at the same time: .)
Question 2
A 7-bit Hamming word arrives with failed checks at group 1 and group 2 only. Which position is wrong?
Show answerHide
The syndrome is : position 3 (it belongs to the groups of and and to no other group).
Question 3
Why can the extended Hamming code detect two errors while the plain one cannot?
Show answerHide
The extra overall parity bit separates the cases: one error makes the overall parity odd, two errors leave it even. Two errors give a non-zero syndrome with even overall parity, which cannot be a single error.
Revision card
One-screen revision
- Distance : detects , corrects ; correct + detect if .
- Check bits at 1, 2, 4, 8; group of = positions with bit set; even parity.
- Syndrome = binary number of the failed groups = position of the error. .
- Plain: corrects 1, mistakes 2 for something else. Extended (+ overall parity): , SEC-DED.
References
- ·Tanenbaum, Wetherall, Computer Networks (5th edition), Section 3.2 Error detection and correction (error-correcting codes, Hamming codes)
- ·Forouzan, Data Communications and Networking (5th edition), Chapter 10 Error detection and correction (block coding, Hamming distance, Hamming code)
- ·Kurose, Ross, Computer Networking: A Top-Down Approach, Section 6.2 Error-detection and -correction techniques (parity checks)
Verification
scripts/enum-checks/cn_hamming_check.py: (1) for 1 to 11 data bits the codewords produced by the encoder (scripts/hamming_lib.py) are exactly the null space of the parity-check matrix whose column p is the binary number p (all 2^n words tried), 2^m of them, minimum distance 3; (2) every single flipped bit of every codeword (all data words up to 8 bits, random ones for 11) gives a syndrome equal to the position and is corrected, for even and for odd parity; every pair of flipped bits gives a syndrome equal to the exclusive or of the two positions and a wrong result; (3) the extended code has minimum distance 4, corrects every single error, detects EVERY double error (never mis-corrects one) and mis-corrects some triple errors (120480 in the cases tried); (4) the least number of check bits equals the least r for which a systematic single-error-correcting code exists (exhaustive search over all check matrices for 1 to 4 data bits), the length-(2^r - 1) codes are perfect; (5) on six small codes the detection of d-1 and correction of floor((d-1)/2) errors was verified with all error patterns, the bounds are tight, and correcting t and detecting s errors (s at least t) works exactly when d is at least s + t + 1; (6) the numbers and words of the page. tests/cn-hamming-traces.test.ts re-encodes the words and recomputes the tables on integers in TypeScript. CONVENTIONS: positions are numbered from the RIGHT, so a 12-bit word reads d8 d7 d6 d5 c8 d4 d3 d2 c4 d1 c2 c1 with the check bit c1 at position 1; some books number the positions from the left, which only changes how the word is written; even parity unless stated; the data bits fill the non-power-of-two positions in order, d1 at the lowest. The memory example (8 check bits for 64 data bits) is computed from the same formula, not taken from a hardware manual. Textbook chapter and section numbers are from memory and need verification. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (CRC remainders, binomial tail, checksums); long C programs were not re-read line by line (they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose). No defect found. Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)