Hamming(7,4) Encoder/Decoder

Add error-detecting parity bits using Hamming code

Encode 4 data bits into a 7-bit Hamming(7,4) codeword, and decode 7-bit codewords with single-error correction. Computes parity bits p1, p2, p4 and locates the flipped bit from the syndrome. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

What is Hamming(7,4)?

It is a linear error-correcting code that protects 4 data bits with 3 parity bits, producing a 7-bit codeword. It can detect up to two-bit errors and correct any single-bit error.

This tool implements the classic Hamming(7,4) error-correcting code. Encoding takes 4 data bits and adds 3 parity bits to form a 7-bit codeword that can survive a single-bit flip. Decoding takes a (possibly corrupted) 7-bit codeword, computes a syndrome, corrects one bit if needed, and recovers the original 4 data bits.

Why Hamming codes matter

Digital storage and transmission are not perfectly reliable. Cosmic rays can flip a single bit in RAM (a “soft error”). Noisy transmission channels corrupt individual bits. The first industrial response to these failures was simple parity — add one bit so the total number of 1s is always even. This detects a single-bit error (the parity is now wrong) but cannot correct it, because parity alone does not identify which bit flipped.

Richard Hamming invented his family of codes in 1950 while working at Bell Labs, frustrated that the punched-card reader at the lab’s computer kept causing his weekend calculations to restart on errors. His insight was to use multiple overlapping parity checks, each covering a different subset of bits, so that the pattern of which checks fail uniquely identifies the corrupted position. The Hamming(7,4) code — 4 data bits, 3 parity bits — is the simplest case and the one most commonly taught.

Modern error-correcting codes (Reed-Solomon, LDPC, polar codes) are far more powerful, but Hamming(7,4) remains the clearest demonstration of the core idea: structured redundancy that does not just detect errors but locates them.

How it works

The 7 positions are numbered 1–7. Positions that are powers of two — 1, 2 and 4 — hold the parity bits p1, p2, p4; the remaining positions 3, 5, 6, 7 hold the data bits.

Each parity bit covers the positions whose binary index includes its bit:

p1 (pos 1) covers positions 1, 3, 5, 7
p2 (pos 2) covers positions 2, 3, 6, 7
p4 (pos 4) covers positions 4, 5, 6, 7

Each parity bit is chosen so the sum of the bits it covers is even (even parity).

Decoding recomputes the three parity checks over the received word. The check results, written as c4 c2 c1, form a syndrome. If the syndrome is 000, no single-bit error is detected. Any other value is the position number of the corrupted bit — so a syndrome of 101 (= 5 in binary) means position 5 was flipped, and the decoder flips it back before extracting the data.

Worked example

Encode data 1011 (d1=1, d2=0, d3=1, d4=1):

Positions:  _  _  1  _  0  1  1   (underscores = parity slots at 1, 2, 4)
p1 covers positions 1,3,5,7 = p1,1,0,1  → p1 = 0  (even parity)
p2 covers positions 2,3,6,7 = p2,1,1,1  → p2 = 1  (even parity)
p4 covers positions 4,5,6,7 = p4,0,1,1  → p4 = 0  (even parity)
Codeword:   0  1  1  0  0  1  1   (positions 1–7)

Now flip bit at position 5 (simulating a transmission error). The decoder recomputes the three checks and finds syndrome 101 = 5, identifies position 5 as the error, flips it back, and recovers 1011.

Notes

  • The decoder reports the syndrome and corrected position so you can trace the mechanism step by step.
  • Hamming(7,4) corrects exactly one flipped bit. A two-bit error produces a non-zero syndrome but the decoder will miscorrect — pointing to a wrong single position.
  • Input must be exactly 4 bits for encoding or 7 bits for decoding; the tool validates length and rejects non-binary input.