Hamming Code Calculator

Encode, flip a bit, and see the syndrome name the position it flipped — for (7,4), SECDED, or any data word you choose.

Code

(7, 4) — 3 inner parity bits.

Data bits

Codeword produced

Select any cell to flip that bit and inject an error. The syndrome below points straight back at it.

Note:

Syndrome 0. No error detected.

Every parity check passes, so the received word is a codeword and the payload is taken as it stands.

Codeword

0110011

Received

0110011

no errors injected

Syndrome 0no error

Coverage grid

Which positions each parity bit checks. A position is checked by parity bit p_k exactly when bit k of its 1-indexed position number is set, which is what makes the syndrome read out as the position of the flipped bit.
Parity bit1234567
p1checkednot checkedcheckednot checkedcheckednot checkedchecked
p2not checkedcheckedcheckednot checkednot checkedcheckedchecked
p4not checkednot checkednot checkedcheckedcheckedcheckedchecked
Bit0110011

Hamming (7,4) encode

Hamming (7,4): 3 parity bits, because 2^3 = 8 ≥ k + r + 1 = 8.

Data word1011

Parity bits occupy the power-of-two positions; the payload fills the rest in order. — Hamming (7,4) encode
PositionBinary indexHoldsBit
1001p10
2010p21
3011d11
4100p40
5101d20
6110d31
7111d41

Each parity bit is the XOR of the data bits it covers, so every check comes out even.

p11 ⊕ 0 ⊕ 1 = 0covers positions 3, 5, 7

p21 ⊕ 1 ⊕ 1 = 1covers positions 3, 6, 7

p40 ⊕ 1 ⊕ 1 = 0covers positions 5, 6, 7

Codeword0110011Highlighted cells are parity bits.

Warning:

Positions are 1-indexed: the first character of the codeword is position 1.

Source: R. W. Hamming, Bell System Technical Journal 29(2):147–160 (1950)

Hamming (7,4) decode

Received0110011Highlighted cells are parity bits.

Each check XORs its coverage set, the parity bit included. — Hamming (7,4) decode
CheckParity bitPositions checkedResult
s0p11, 3, 5, 70 (passes)
s1p22, 3, 6, 70 (passes)
s2p44, 5, 6, 70 (passes)

p1 check0 ⊕ 1 ⊕ 0 ⊕ 1 = 0checks positions 1, 3, 5, 7

p2 check1 ⊕ 1 ⊕ 1 ⊕ 1 = 0checks positions 2, 3, 6, 7

p4 check0 ⊕ 0 ⊕ 1 ⊕ 1 = 0checks positions 4, 5, 6, 7

Syndrome S000₂ = 0read with s₀ as the least significant bit

Every check passes and the syndrome is 0: no error detected.

Data word1011

Warning:

A single-error-correcting code cannot tell a double error from a single error; the syndrome then points at an innocent bit.

Source: R. W. Hamming, Bell System Technical Journal 29(2):147–160 (1950)

Generator and parity-check matrices over GF(2)

Hamming (7,4) matrices over GF(2)

Position-ordered parity-check matrix H: column c is position (c + 1) written in binary, least significant bit in row 0.

H (position-ordered) — Hamming (7,4) matrices over GF(2)
1234567
1010101
0110011
0001111
G (position-ordered): row i is the codeword of the unit data word e_i — Hamming (7,4) matrices over GF(2)
1234567
1110000
1001100
0101010
1101001

Systematic form puts the payload first: G = [I_k | P] and H = [Pᵀ | I_{n−k}], so G·Hᵀ = P + P = 0.

G = [I_k | P] — Hamming (7,4) matrices over GF(2)
1234567
1000110
0100101
0010011
0001111
H = [Pᵀ | I_{n−k}] — Hamming (7,4) matrices over GF(2)
1234567
1101100
1011010
0111001
Warning:

Encoding is c = m·G and the syndrome is s = H·rᵀ, both over GF(2).

Source: R. W. Hamming, Bell System Technical Journal 29(2):147–160 (1950)

Note:

Notation used on this page

  • Positions are 1-indexed and the first character of the codeword is position 1. SECDED adds an overall parity bit numbered 0, written first.
  • The syndrome is read with the check of p1 as its least significant bit, so its decimal value is the position of a single flipped bit.
  • Parity is even throughout: each check XORs its coverage set, the parity bit included, and a passing check gives 0.

Start from a worked example

What a Hamming code does

A Hamming code adds parity bits in positions chosen so that the failing checks, read as a binary number, spell the position of the bit that went wrong. That is the whole idea, and everything below is the mechanism that makes it true.

A single parity bit can tell you that a byte is damaged; it cannot tell you which bit. A Hamming code spends a few more bits and buys the position. It is the opposite move to Huffman coding, which removes redundancy — a real system compresses first and then adds error correction back, in that order.

Where the parity bits go

Parity bits sit at the powers of two — positions 1, 2, 4, 8, 16 — and the data fills every other position in ascending order. The reason is that position 2ʲ is the only position covered by parity bit j and by no other parity bit, so each parity bit can be solved for on its own without simultaneous equations.

Parity bit j covers every position whose binary expansion has bit jset. Position 1 covers 1, 3, 5, 7…; position 2 covers 2, 3, 6, 7…; position 4 covers 4, 5, 6, 7…. Write each position number in binary and read down its column and you have the coverage grid.

Computing the parity bits

For the data word 1011, the data lands at positions 3, 5, 6 and 7 as d₁=1, d₂=0, d₃=1, d₄=1. Then p1 = d(3) ⊕ d(5) ⊕ d(7) = 1 ⊕ 0 ⊕ 1 = 0; p2 = d(3) ⊕ d(6) ⊕ d(7) = 1 ⊕ 1 ⊕ 1 = 1; p4 = d(5) ⊕ d(6) ⊕ d(7) = 0 ⊕ 1 ⊕ 1 = 0. The codeword is 0110011.

When encoding, a parity bit is computed from the other positions in its coverage set and excludes itself, because its value is what is being solved for. When checking, the same XOR includes the parity bit, so a clean word gives 0. Mixing the two conventions is the commonest way to get a syndrome that is right but inverted.

Decoding: the syndrome

Recompute every check over the received word, including each parity bit’s own position. Each check gives a bit; assemble them with p1’s check as the least significant. The syndrome, read as a binary number, is the position of the flipped bit.

Flip position 5 of 0110011 and you receive 0110111. The p1 check fails, the p2 check passes, the p4 check fails, so the syndrome is 101₂ = 5 — the position that changed. Flipping bit 5 back recovers the codeword and the data. A syndrome of 0 means no single-bit error was detected. Click any cell of the codeword above to try it.

Hamming distance, weight, and what a code can promise

The Hamming distance d(x,y) between two words of equal length is the number of positions where they differ, which is the weight — the number of ones — of x ⊕ y. For a linear code, whose codewords are closed under XOR, the minimum distance d_min is simply the smallest weight of any non-zero codeword, which makes it computable without comparing every pair.

Everything a block code can promise follows from d_min: it detects up to d_min − 1 errors, and it corrects up to ⌊(d_min − 1)/2⌋.

What a minimum distance buys
d_minErrors detectedErrors correctedExample
100every word is a codeword
210a single parity bit
321the (7,4) Hamming code
431SECDED: correct one and detect two at once
542
652
763
873

The (7,4) code in full

Four data bits give 16 codewords out of the 128 possible seven-bit words. Their weight enumerator is one word of weight 0, seven of weight 3, seven of weight 4 and one of weight 7, so d_min = 3 and the code corrects one error.

(7,4) is a perfect code: 16 × (1 + 7) = 128 = 2⁷. Every seven-bit word is either a codeword or exactly one bit away from exactly one codeword — the radius-1 balls around the 16 codewords tile the whole space with nothing left over. That is why every non-zero syndrome names a real position and none is spare.

Why (7,4) mis-corrects a double error

Because the code is linear, flipping positions a and bproduces the syndrome a ⊕ b — which is non-zero, and is itself a valid position. The decoder therefore “corrects” a third, innocent bit and hands back a word that is now three bits from the truth. All 21 two-bit error patterns on 0110011 behave this way; none of them recovers the data.

A (7,4) code cannot tell a double error from a single error — it will “correct” the wrong bit. That is what the extended (8,4) SECDED code fixes.

SECDED

Adding one more parity bit over the whole codeword raises d_min from 3 to 4 and separates the two cases, because a single error changes the overall parity and a double error does not. The result has four distinct outcomes, and they stay four.

The SECDED decision table
OutcomeSyndromeOverall parityAction
No error0passesaccept the word as received
Single errornon-zerofailsflip the bit at the syndrome position and accept
Error in the overall parity bit0failsthe data is intact; only the extra bit was hit
Double errornon-zeropassesdetected, not correctable — no repaired word is offered

The fourth row offers no corrected codeword, and that is the point: a distance-4 code can prove two bits are wrong and cannot say which two, because the received word sits exactly halfway between two codewords. A tool that repairs it anyway is emitting a confident wrong answer.

Three or more errors exceed this code’s guarantee and will be silently mis-diagnosed. That is not a defect in the implementation; it is what “single error correction, double error detection” means.

Generator and parity-check matrices

The same code has a matrix form over GF(2), where addition is XOR. The parity-check matrix H has one column per position and its column for position i is simply i written in binary — which is another way of saying that the syndrome is the position. The generator matrix G turns a message into a codeword: c = m·G, and the check is s = H·rᵀ.

Two conventions are in use and both appear above. In the positionalform the columns stay in position order 1…n, which is what makes the syndrome readable as a position. In the systematic form the columns are permuted so that G is [I | P] and H is [Pᵀ | I], which is what linear-algebra courses show. They generate the same code, and in both conventions G · Hᵀ = 0.

Bigger codes: (15,11), (31,26), and any (n,k)

The number of parity bits r for k data bits is the smallest rwith 2ʳ ≥ k + r + 1: the syndrome has to be able to name every position plus the “no error” case. When the bound is met with equality the code is perfect — (7,4), (15,11), (31,26) — and 2ᵏ × (1 + n) = 2ⁿ holds exactly. When it is not, the code is shortened: it still works, some syndromes simply never occur.

The code for a given number of data bits
Data bits kParity bits rCodeRate k/n
12(3,1)33%
43(7,4)57%
84(12,8)67%
114(15,11)73%
265(31,26)84%
576(63,57)90%
647 + 1 overall(72,64) SECDED89%

Rate: what the correction costs

The rate k/n is the fraction of the transmitted bits that carry your data: 4/7 ≈ 57% for (7,4), 11/15 ≈ 73%, 26/31 ≈ 84%, and 64/72 ≈ 89% for the SECDED code that ECC memory uses. Bigger codes are cheaper per bit because the parity count grows logarithmically while the data count grows linearly.

They are also worse at bursts: one code word can still only survive one error, however long it is, so a burst that hits two adjacent bits defeats a big code just as easily as a small one. Burst detection is a CRC’s job. The information-theoretic framing — redundancy as the price of reliability — is Shannon’s, and the entropy calculator measures it in the same unit.

Hamming's 1950 paper, in two sentences

Hamming was running weekend batch jobs on a relay machine that halted on a single detected error, so a two-day run could be lost to one bad relay and nobody was there to restart it. His 1950 paper introduced the distance metric between codewords and the positional construction on this page, which together turn “something is wrong” into “bit five is wrong”.

Notation used on this page

  • Positions are numbered from 1, left to right, so position 1 is the leftmost character of the codeword string.
  • Parity bits sit at every power of two and data bits fill the rest in ascending position order.
  • Parity is even throughout.
  • A check s_j includes the parity bit’s own position, and the syndrome is S = Σ s_j·2ʲ.
  • SECDED prepends an overall parity bit numbered 0, so the SECDED word is indexed from 0 while the inner code stays indexed from 1; both indices are printed above the strip.
  • is XOR, and matrices are over GF(2), where addition is XOR.

Sources

Worked examples