Hamming Code Calculator

Hamming(7,4): encode 4 bits, decode 7-bit words, locate and fix single-bit errors.

Technology and NetUpdated 2026-10-172 min read
Hamming Code Calculator
Codeword (corrected)
...
Data bits...
Parity bits...
Syndrome...
Error position...

What it does

Hamming codes, published by Richard Hamming in 1950, were the first practical single-error correcting codes. The classic Hamming(7,4) takes 4 data bits and adds 3 parity bits to make 7, enough to not just detect a flipped bit but name its position.

The layout follows powers of two: positions 1, 2, and 4 hold parity bits P1, P2, P4, while positions 3, 5, 6, and 7 hold the data bits d1 through d4. Each parity bit covers the positions whose index has that bit set: P1 checks 1, 3, 5, 7; P2 checks 2, 3, 6, 7; P4 checks 4, 5, 6, 7.

Decoding re-runs the three checks on the received word. The results, read as a three-bit binary number, are the position of the flipped bit, zero if everything is clean. Flip it back and the data reads out of positions 3, 5, 6, and 7.

One flip is always corrected; two flips can masquerade as a third position and 'correct' the wrong bit. Real links bolt on CRCs, retransmission, or an extra overall parity bit (SECDED) to cover that gap.

How to use

  1. Pick encode or decode.
  2. Encoding: enter exactly 4 binary data bits.
  3. Decoding: enter the 7 received bits.
  4. Read the codeword first.
  5. Read the parity bits or syndrome.
  6. Check the error position and corrected data.

Formula

P1 = d1 ^ d2 ^ d4 (covers 3, 5, 7). P2 = d1 ^ d3 ^ d4 (3, 6, 7). P4 = d2 ^ d3 ^ d4 (5, 6, 7). Syndrome: S1 = y1^y3^y5^y7, S2 = y2^y3^y6^y7, S4 = y4^y5^y6^y7; error position = S4 S2 S1 read as a binary number.
d1..d4
The four data bits at positions 3, 5, 6, 7.
P1, P2, P4
Parity bits at positions 1, 2, 4.
y1..y7
Received word bits.
syndrome
Three check results S1, S2, S4 read as a position.

Parity bits live in seats whose index is a power of two, and each check covers positions whose index has that power set. Reading the syndrome S4 S2 S1 as binary gives the bad position directly, which is the elegance of Hamming's design; zero means no detected error. Two-bit errors can alias into a valid single position, so Hamming(7,4) alone is not a double-error detector.

Worked example

Example: encode 1010, then repair a flipped bit

Data bits 1010; the received word has bit 7 flipped: 1011011.

  1. P1 = 1^0^0 = 1, P2 = 1^1^0 = 0, P4 = 0^1^0 = 1.
  2. Codeword: 1 0 1 1 0 1 0 = 1011010.
  3. Received 1011011: checks return S1 = 1, S2 = 1, S4 = 1.
  4. Syndrome 111 is position 7; flip that bit back.
  5. Corrected: 1011010; data at positions 3, 5, 6, 7: 1010.
  6. Tool shows codeword 1011010, data 1010, syndrome 111, error position 7.

Reference table

Encoding the classic examples
DataParity P1 P2 P4Codeword
00000 0 00000000
00011 1 11101001
01010 1 00100101
10101 0 11011010
11111 1 11111111

Limits

  • Corrects one flipped bit; two flips can look like a different single error.
  • Fixed Hamming(7,4): exactly 4 data bits per word; longer messages chain words.
  • No burst or erasure handling; real protocols add CRCs and retransmission.
  • The decoder assumes at most one error; more than that produces confident nonsense.

Common mistakes

  • Reading the syndrome in the wrong bit order; it is S4 S2 S1, not the check order.
  • Assuming two-bit errors are safe rather than aliasing.
  • Forgetting that positions count from 1, not 0.
  • Reading data from seats 1, 2, 4 instead of 3, 5, 6, 7.
  • Encoding with even-parity conventions from other texts; this tool uses the classic odd-count XOR checks.

Try it yourself

Quick checks. Answers sit tucked under each question.

Encode 1111?

Parity 1 1 1; codeword 1111111.

Receive 0011010?

Syndrome 001 is position 1; correct to 1011010, data 1010.

Receive 1011010?

Syndrome 000: clean; data 1010.

Questions

How does Hamming(7,4) work?

Four data bits get three parity bits in positions 1, 2, and 4. Each parity covers a distinct pattern of positions, so any single flip produces a syndrome that names its location.

Why is the syndrome the position?

Each check bit covers positions whose index has its bit set; the three results together form the binary index of the bad bit, with 000 meaning clean.

Can it fix two errors?

No. Two flips produce a syndrome matching some third position and the decoder 'fixes' the wrong bit. An overall parity bit turns it into SECDED: correct one, detect two.

Where is Hamming code used today?

It taught a generation of engineers and lives in legacy and light-duty links; modern storage and networks lean on stronger codes like Reed-Solomon, BCH, and LDPC, often combined with CRCs.

How do I extend to longer data?

General Hamming codes use more parity seats: 8 data bits need 4 parity (12,8), and the same power-of-two layout and syndrome logic scales.

Does the tool accept spaces?

Yes, spaces are stripped; entries must be exactly 4 bits to encode or 7 bits to decode.

Sources

How this page was checked. The formula above was matched against the cited sources, and the worked example was run through the tool on this page before publishing. If you spot an error, contact us so we can fix it and note the correction.