G27
Self-checkedTier 2 13 min readcn.error-correction-hamming

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 1011010110 and 1110011100 it is 2.

Code with mm data bits and rr check bits: the set of the 2m2^m valid codewords of length n=m+rn = m + r out of all 2n2^n words. Minimum distance dd of the code: the smallest distance between two different codewords.

What the distance buys.

  • It detects every error pattern of up to d−1d - 1 flipped bits (no such pattern can turn a codeword into another codeword).
  • It corrects every pattern of up to t=⌊(d−1)/2⌋t = \lfloor (d - 1) / 2 \rfloor flipped bits (the received word is nearer to the sent codeword than to any other).
  • It corrects up to tt and at the same time detects up to s≥ts \ge t errors if d≥s+t+1d \ge s + t + 1.

Hamming code. Number the positions 1,2,…,n1, 2, \dots, n from the right. The positions that are powers of two (1, 2, 4, 8, ...) hold the check bits c1,c2,c4,c8,…c_1, c_2, c_4, c_8, \dots; all other positions hold the data bits d1,d2,…d_1, d_2, \dots in order. The check bit c2kc_{2^k} is chosen so that the group of positions whose binary index has bit kk 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 bitPositions in its group
c1c_11, 3, 5, 7, 9, 11
c2c_22, 3, 6, 7, 10, 11
c4c_44, 5, 6, 7, 12
c8c_88, 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 nn positions, or there is none: n+1n + 1 cases, each needing its own syndrome out of 2r2^r. So 2r≥m+r+12^r \ge m + r + 1.

How it works

Encoding

Encoding 1011 with a (7,4) Hamming code: start

Computed by a script, not typed
check bitPositions in its groupData bits thereOnes among themValue 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.

0 / 4
computed by script: encoding, syndromes and distances of scripts/hamming_lib.py. python3 scripts/gen-cn7-traces.py

The data d4d3d2d1=1011d_4 d_3 d_2 d_1 = 1011 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 c1c_1 and c2c_2, 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
stepPositionsBits received thereOnesResult
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.

0 / 5
computed by script: encoding, syndromes and distances of scripts/hamming_lib.py. python3 scripts/gen-cn7-traces.py

Position 5 (101 in binary) is in the groups of c1c_1 and c4c_4, so exactly those two checks fail, and the failed checks read 101=5101 = 5 (check 4 on the left). In general, flipping position pp spoils exactly the groups that contain pp, which are the groups of the one-bits of pp: the syndrome is the binary number pp itself. If the syndrome is 0 either there is no error or the pattern is invisible.

Two flipped bits at positions pp and qq give the syndrome p⊕qp \oplus q, 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 bitsCheck bits rCodeword lengthData bits / lengthCheck bits with double-error detectionLength 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.

0 / 11
computed by script: encoding, syndromes and distances of scripts/hamming_lib.py. python3 scripts/gen-cn7-traces.py

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 2r−12^r - 1 (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
codeLength nCodewordsMinimum distance dDetectsCorrectsBalls 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.

0 / 6
computed by script: encoding, syndromes and distances of scripts/hamming_lib.py. python3 scripts/gen-cn7-traces.py

Each row was found by comparing all pairs of codewords and confirmed with all error patterns. Observe: an even-parity bit gives d=2d = 2 (detects one error, corrects none), the repetition code 000, 111 gives d=3d = 3, and the Hamming code gives d=3d = 3 however long it is. The balls of radius 1 around the codewords of the Hamming (7,4)(7,4) code cover all 27=1282^7 = 128 words, so it is perfect; the extended code with d=4d = 4 is not (it has dd 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 (4≥2+1+14 \ge 2 + 1 + 1).

Single-error correction, double-error detection: start

Computed by a script, not typed
caseReceived wordSyndromeParity of all 8 bitsDecoder saysOutcome
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.

0 / 5
computed by script: encoding, syndromes and distances of scripts/hamming_lib.py. python3 scripts/gen-cn7-traces.py

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

Easy

How many check bits do 8 data bits need? 23=8<8+3+1=122^3 = 8 < 8 + 3 + 1 = 12, 24=16≥8+4+1=132^4 = 16 \ge 8 + 4 + 1 = 13: 4. The codeword has 12 bits; the check bits are at positions 1, 2, 4 and 8; the layout from the right is d8d7d6d5 c8 d4d3d2 c4 d1 c2 c1d_8 d_7 d_6 d_5\ c_8\ d_4 d_3 d_2\ c_4\ d_1\ c_2\ c_1. The check bit c8c_8 covers positions 8 to 12.

EX 2Encode and correct a 12-bit word

Medium

Data d8…d1=10110010d_8 \dots d_1 = 10110010. Place them: d1=0d_1 = 0 at position 3, d2=1d_2 = 1 at 5, d3=0d_3 = 0 at 6, d4=0d_4 = 0 at 7, d5=1d_5 = 1 at 9, d6=1d_6 = 1 at 10, d7=0d_7 = 0 at 11, d8=1d_8 = 1 at 12. c1c_1 covers 3, 5, 7, 9, 11 with bits 0, 1, 0, 1, 0: two ones, so c1=0c_1 = 0. c2c_2 covers 3, 6, 7, 10, 11: bits 0, 0, 0, 1, 0: one one, c2=1c_2 = 1. c4c_4 covers 5, 6, 7, 12: bits 1, 0, 0, 1: two ones, c4=0c_4 = 0. c8c_8 covers 9, 10, 11, 12: bits 1, 1, 0, 1: three ones, c8=1c_8 = 1. The codeword, positions 12 to 1: 101110010010. If position 9 is flipped in transit, the received word is 101010010010: the checks of c1c_1 and c8c_8 fail (position 9 is in both), the syndrome is 1001=91001 = 9, flip position 9 and the codeword is back.

EX 3Decode a received word

Medium

The 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 c1c_1 (positions 1, 3, 5, 7, 9, 11) holds 0, 1, 0, 1, 0, 1: three ones, fails. Group c2c_2 (2, 3, 6, 7, 10, 11) holds 0, 1, 1, 1, 1, 1: five ones, fails. Group c4c_4 (4, 5, 6, 7, 12) holds 1, 0, 1, 1, 0: three ones, fails. Group c8c_8 (8, 9, 10, 11, 12) holds 0, 0, 1, 1, 0: two ones, passes. The syndrome, check 8 first, is 0111=70111 = 7. Flip position 7: the corrected word is 011000101100 and the data bits d8…d1d_8 \dots d_1 (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 {00000,01110,10101,11011}\{00000, 01110, 10101, 11011\}: the six pairwise distances are 3, 3, 3, 3, 4 and 4, so d=3d = 3: it detects 2 errors and corrects 1. (b) "A code has d=5d = 5. How many errors can it detect, and correct?" Detect 4, correct 2. Used together: correct 2 and detect 2 (2+2+1=52 + 2 + 1 = 5), or correct 1 and detect 3 (1+3+1=51 + 3 + 1 = 5). (c) "How many check bits for 16 data bits? For 57?" 5 (25=32≥222^5 = 32 \ge 22; 24=16<212^4 = 16 < 21) and 6 (64≥6464 \ge 64). (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 dd: detects d−1d - 1, corrects ⌊(d−1)/2⌋\lfloor (d-1)/2 \rfloor; corrects tt and detects s≥ts \ge t if d≥s+t+1d \ge s + t + 1.
  • Hamming code: check bits at positions 1, 2, 4, 8, ... Group of c2kc_{2^k} = positions whose index has bit kk 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: 2r≥m+r+12^r \ge m + r + 1. For mm = 4, 8, 11, 16, 26, 57, 64: rr = 3, 4, 4, 5, 5, 6, 7.
  • Plain Hamming: d=3d = 3, corrects 1 and does not recognise 2. Extended (one more overall parity bit): d=4d = 4, corrects 1 and detects 2 (SEC-DED).

Traps and GATE patterns

Connections

Error detection and correction are two uses of the same redundancy, the distance between valid words: a code with distance dd detects d−1d - 1 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

GATE 2021 · MCQ · 2 marksQ8 · page 534 of the book
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
GATE 2017 · MCQQ12 · page 535 of the book
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 answer

It detects 5 and corrects ⌊5/2⌋=2\lfloor 5/2 \rfloor = 2. (It can correct 2 and detect up to 3 at the same time: 2+3+1=62 + 3 + 1 = 6.)

Question 2

A 7-bit Hamming word arrives with failed checks at group 1 and group 2 only. Which position is wrong?

Show answer

The syndrome is 011=3011 = 3: position 3 (it belongs to the groups of c1c_1 and c2c_2 and to no other group).

Question 3

Why can the extended Hamming code detect two errors while the plain one cannot?

Show answer

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 dd: detects d−1d-1, corrects ⌊(d−1)/2⌋\lfloor (d-1)/2 \rfloor; correct tt + detect ss if d≥s+t+1d \ge s + t + 1.
  • Check bits at 1, 2, 4, 8; group of c2kc_{2^k} = positions with bit kk set; even parity.
  • Syndrome = binary number of the failed groups = position of the error. 2r≥m+r+12^r \ge m + r + 1.
  • Plain: corrects 1, mistakes 2 for something else. Extended (+ overall parity): d=4d = 4, 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