Skip to main content

Hamming Code Explained: Encoding, Syndromes, and Fixing One Flipped Bit

How a Hamming code places parity bits at power-of-two positions, computes a syndrome to locate a single-bit error, and corrects it without retransmission.

Published By Li Lei
#hamming-code #error-correction #parity #ecc #computer-science

Hamming Code Explained: Encoding, Syndromes, and Fixing One Flipped Bit

A single bit flips somewhere between the sender and the receiver. A cosmic ray clips a DRAM cell, a noisy radio link smears a symbol, a long cable picks up interference. Without help, the receiver has no idea anything went wrong. A Hamming code fixes that: it adds a few parity bits so the receiver can not only notice a single-bit error but pinpoint and repair it on its own, with no request to resend.

Richard Hamming designed the scheme in 1950 because he was tired of a card reader that would halt his weekend batch jobs on the first error. His insight is genuinely clever, and once you see how the parity positions line up with the binary index of each bit, the whole thing clicks. This post walks through that idea, encodes four data bits by hand, then deliberately breaks a bit and watches the code find it.

Why parity bits live at positions 1, 2, 4, 8

Number every position in the codeword from 1, starting at the leftmost bit. Parity bits occupy the power-of-two positions: 1, 2, 4, 8, 16, and so on. Everything else holds your data, filled in order.

The reason for those positions is the part worth slowing down for. Each parity bit owns one bit of the position number itself. Write each index in binary and look at which bit is set:

  • P1 (position 1 = 001) checks every position whose index has the 1s bit set: 1, 3, 5, 7, 9…
  • P2 (position 2 = 010) checks every position with the 2s bit set: 2, 3, 6, 7, 10, 11…
  • P4 (position 4 = 100) checks the 4s group: 4, 5, 6, 7, 12, 13…
  • P8 checks the 8s group, and so on.

Each data position is therefore covered by a unique combination of parity bits, because every number has a unique binary expansion. Position 5 (101) is covered by P1 and P4. Position 6 (110) is covered by P2 and P4. No two positions share the exact same set of checks. That uniqueness is what lets the decoder turn "which checks failed" straight into "which position broke."

A Hamming code needs r parity bits to protect m data bits, where 2^r ≥ m + r + 1. For 4 data bits, r = 3 works (2^3 = 8 ≥ 4 + 3 + 1), giving the classic (7,4) code: seven bits total, four of them data.

Encoding four data bits by hand

Take the data bits 1011. The seven positions fill like this — parity slots empty for now:

Position:  1   2   3   4   5   6   7
Bit:       P1  P2  1   P4  0   1   1

Now compute each parity bit as even parity (make the count of 1s in its group even).

  • P1 covers positions 1, 3, 5, 7 → bits P1, 1, 0, 1. The data bits there are 1, 0, 1 = two 1s, already even, so P1 = 0.
  • P2 covers positions 2, 3, 6, 7 → bits P2, 1, 1, 1. The data bits are 1, 1, 1 = three 1s, odd, so P2 = 1 to make four.
  • P4 covers positions 4, 5, 6, 7 → bits P4, 0, 1, 1. The data bits are 0, 1, 1 = two 1s, even, so P4 = 0.

Drop those in and read the full codeword left to right:

Position:  1   2   3   4   5   6   7
Bit:       0   1   1   0   0   1   1

The codeword is 0110011. That matches the (7,4) result the Hamming Code Calculator produces for input 1011, with the per-parity coverage spelled out so you can check every group yourself instead of trusting one number.

Computing the syndrome to locate an error

Now suppose the codeword 0110011 travels across a link and arrives as 0110111 — position 5 flipped from 0 to 1. The receiver does not know that yet. It recomputes each parity check on what it received, and for every check that fails, it adds that check's parity position to a running total. That total is the syndrome.

Check the received bits 0 1 1 0 1 1 1:

  • P1 group (positions 1, 3, 5, 7) = 0, 1, 1, 1 → three 1s, odd, fails. Add 1.
  • P2 group (positions 2, 3, 6, 7) = 1, 1, 1, 1 → four 1s, even, passes.
  • P4 group (positions 4, 5, 6, 7) = 0, 1, 1, 1 → three 1s, odd, fails. Add 4.

Syndrome = 1 + 4 = 5. The number is literally the 1-based index of the broken bit. Flip position 5 back from 1 to 0 and you recover 0110011, then strip the parity positions to extract the original data 1011. A syndrome of 0 would mean every check passed and no single-bit error is present.

I find this still a little magical even knowing the mechanism. The two failing checks are exactly the two parity bits whose positions (1 and 4) sum to 5 — and position 5 is the only data position covered by both P1 and P4 and nothing else. The arithmetic and the geometry agree because the position numbers were the parity groups all along.

Where Hamming codes show up

The (7,4) code is a teaching example, but the same construction scales and ships in real hardware. ECC memory in servers and workstations is the most common place: every cache line carries Hamming-style parity so a stray bit flip from a cosmic ray or marginal cell gets corrected before it reaches your program. NAND flash controllers, spacecraft telemetry, QR codes (which use the related Reed–Solomon family), and low-bandwidth radio links all lean on forward error correction so the receiver fixes damage without asking for a resend — which matters when the round trip is a satellite away.

There's a hard limit worth stating plainly: a plain Hamming code corrects exactly one flipped bit per word. Two flips in the same word produce a nonzero syndrome that points at a third, innocent position, silently making things worse. The fix is SECDED — single-error-correct, double-error-detect — which prepends one overall parity bit covering the entire codeword. With the syndrome plus that extra bit you can distinguish three cases: clean, one error (corrected), two errors (detected and refused). That refusal is the point: server memory logs an uncorrectable double-bit event rather than handing back corrupt data.

Working through it yourself

The fastest way to internalize this is to encode a value, flip a bit, and trace the syndrome back to the position you broke — then do it again with SECDED on and flip two bits to watch the code give up safely. The Hamming Code Calculator lays out the bit layout and the coverage list for every parity bit, so each step is auditable rather than a black box. If you also want to convert your data between binary, hex, and decimal while you experiment, the base converter handles that side cleanly.

Once the power-of-two trick lands, Hamming codes stop feeling like magic and start feeling like bookkeeping with a very good filing system: every bit's address is its own error report.


Made by Toolora · Updated 2026-06-13