Hamming Code Calculator
Three extra bits, placed with intent, buy something remarkable: any single error in a 7-bit word can not only be detected but located and corrected. That is the (7,4) Hamming code — the most taught error-correcting code in the world — and this calculator works it both ways: enter 4 data bits to encode them into the protected 7-bit codeword, or enter a received 7-bit word and read its syndrome, which names the guilty position (or certifies the word clean, subject to the single-error assumption the checker is built on).
The Layout That Makes Correction Possible
Number the codeword's positions 1 to 7 and place parity bits at the powers of two — 1, 2, 4 — leaving 3, 5, 6, 7 for data. Each parity bit guards a specific constituency, defined by binary arithmetic: p1 covers every position whose index has the 1-bit set (1, 3, 5, 7), p2 those with the 2-bit set (2, 3, 6, 7), p4 those with the 4-bit set (4, 5, 6, 7), each computed for even parity over its constituency (codewords extended with an overall parity bit exist too — SECDED, below). On receipt, recompute the three parities: the pattern of failures, read as a binary number (s4 s2 s1), is the syndrome — and it equals, exactly, the index of the flipped position, because a single error at position k breaks precisely the parities whose constituencies include k, which are the bits of k itself. Detection schemes merely announce damage; this layout addresses it, which is why a single error needs no retransmission, only arithmetic.
Worked Example: 1011 Becomes 0110011
The defaults run the textbook's canonical case. Data bits 1, 0, 1, 1 take positions 3, 5, 6, 7. Constituency by constituency: positions 3, 5, 7 hold (1, 0, 1) — two ones, already even — so p1 = 0. Positions 3, 6, 7 hold (1, 1, 1) — three ones, odd — so p2 = 1 to restore evenness. Positions 5, 6, 7 hold (0, 1, 1) — two, even — so p4 = 0. The codeword reads, positions 1–7: 0110011. Now corrupt it: flip position 5 in transit and check the received 0110111 (try it in Check mode) — the syndrome computes to 101 binary = 5, naming the wound; flip it back and the data 1011 is restored without a single question asked of the sender. That round trip — encode, corrupt, locate, repair — is the entire promise of forward error correction, executed in seven bits.
The Family: (2^r − 1, 2^r − 1 − r)
(7,4) is the r = 3 member of an infinite family: with r parity bits the code protects blocks of 2^r − 1 bits, of which 2^r − 1 − r are data — (3,1) at r = 2 (the triple-repetition code, in disguise), (15,11) at r = 4, (31,26) at r = 5. The family's efficiency improves with size (the parity tax per data bit falls) while its guarantee stays single-error correction — which is why history scaled the idea rather than the code: modern systems (deep-space links, QR codes, 5G) run Reed–Solomon, LDPC and turbo codes that correct bursts and barrages, but every one of them lives in the conceptual house Hamming built — redundancy, laid out so that errors announce their own addresses. The (7,4) code itself still works for a living inside ECC memory's teaching examples and as the SECDED (single-error-correct, double-error-detect) variant — one extra overall parity bit — in the RAM of servers that cannot afford to be wrong.
Where the Code's Guarantee Ends
Precision about limits is this hub's house style, and the (7,4) code's limits are crisp. Two errors in a word defeat the basic code treacherously: the syndrome points at a third position, "correction" flips it, and the result is a different valid codeword carrying wrong data — error converted into confident error. The SECDED extension (one overall parity bit across all 8 positions) separates the cases: single error (syndrome non-zero, overall parity fails), double error (syndrome non-zero, overall parity holds) — detected, though not corrected. Erasure channels (known-bad positions, as in scratched media with read flags) are kinder: two erasures per word are correctable because their addresses are given. And the code is a block code — burst errors spanning words each take their toll separately, which interleaving exists to redistribute. The calculator's Check mode carries the single-error assumption on its face; quote its corrections under that assumption, as its own note insists.
A Second Encoding, By Hand
One more pass builds the fluency the first example starts: encode 0101. The data bits take positions 3, 5, 6, 7 as 0, 1, 0, 1. Constituencies: positions 3, 5, 7 hold (0, 1, 1) — two ones, even — so p1 = 0. Positions 3, 6, 7 hold (0, 0, 1) — one, odd — so p2 = 1. Positions 5, 6, 7 hold (1, 0, 1) — two, even — so p4 = 0. The codeword is 0100101 — run it through the calculator's Encode mode to watch the same digits fall out, then flip its fourth bit (a parity bit this time) and check: the syndrome returns 4, the parity position itself, and the "correction" restores a word whose data never changed — the code protecting its own machinery with the same three bits, exactly as designed.
Why Seven Is Perfect: The Sphere-Packing Count
The (7,4) code has one more distinction, and it is exact: it is a perfect code. Around each of the 16 codewords, draw the sphere of radius 1 — the codeword plus the 7 words one flip away: 8 words per sphere. Sixteen codewords × 8 = 128 = 2⁷, the entire space of 7-bit words, tiled without overlap or gap: every possible received word is within distance 1 of exactly one codeword, so every single error is correctable with zero ambiguity and zero waste. The sphere-packing (Hamming) bound says no code can do better at these parameters, and perfect codes are rare jewels — beyond the Hamming family, essentially only the binary Golay code keeps the title. Seven bits, sixteen messages, no word wasted: the calculator's little machine is a theorem wearing a widget — and its 1950 paper, "Error Detecting and Error Correcting Codes", remains readable in an afternoon and worth every page.
Related Tools
More from the Other Calculators hub: the Hamming Distance Calculator — the metric that makes the code's guarantee (minimum distance 3) a number — and the live Parity Bit Calculator, the single-bit ancestor whose idea this code triples and arms with addresses.
Frequently Asked Questions
Why are the parity bits at positions 1, 2 and 4?
Because the addressing trick demands it: the syndrome must be able to name any position 1–7 as a 3-bit binary number, so each parity bit must correspond to one binary digit of the position index, and it sits at the position whose index is that digit alone (001, 010, 100). Place the parities anywhere else and the constituencies stop lining up with the index bits — the syndrome becomes a code you'd need a table to read, instead of the address it triumphantly is.
Even parity or odd parity — does it matter?
Only by convention: odd parity (making each constituency's count odd) yields the mirror-image code with identical distance and correction properties. This calculator uses even parity, the textbook standard for (7,4). Encoders and decoders must agree on the convention — a word encoded under even parity will present a non-zero syndrome to an odd-parity checker while being perfectly intact.
What is the code rate, and is it good?
The rate is 4/7 ≈ 0.571 — four data bits per seven transmitted. By modern standards that is lavish redundancy (deep-space and storage codes run far leaner per error corrected), but the comparison misses the point: (7,4) is the smallest code that demonstrates genuine correction, and its rate at larger family sizes climbs ((15,11) ≈ 0.73, (31,26) ≈ 0.84) while classroom clarity stays home at r = 3.
Can I encode longer messages?
Block by block: split the message into 4-bit blocks, encode each into its 7-bit codeword, and concatenate. Each block independently earns single-error correction — an 8-bit byte becomes 14 bits in two blocks. Production systems add interleaving so a burst error spreads across blocks (one error each) instead of concentrating in one word, but the per-block arithmetic is exactly the calculator's.
How does the checker distinguish a parity-bit error from a data-bit error?
It doesn't need to: an error in a parity bit is an error at that bit's position (1, 2 or 4), the syndrome names it like any other, and flipping it restores the word. The data recovered is identical whether the damage struck data or parity — which is the deep economy of the scheme: the redundancy protects itself with the same mechanism, no separate guard for the guards.
Where is this actually used today?
In its SECDED extension (an extra overall parity bit making (8,4)-style words, scaled to 64-bit words as (72,64)), it guards ECC server and workstation memory, correcting the single-bit soft errors that cosmic rays and alpha particles cause in DRAM — errors frequent enough that unguarded large servers measurably corrupt data. The seven-bit ancestor on this page is retired to the classroom, where it remains the finest seven bits in the curriculum.