SOLVETUTORMATH SOLVER

Instrument MI-14-097 · Other

Hamming Code Calculator

Toggle 4 data bits and this instrument computes the three parity bits that turn them into a self-correcting Hamming(7,4) codeword.

Instrument MI-14-097
Sheet 1 OF 1
Rev A
Verified
Type 14 — Electronics & Digital Logic SER. 2026-14097

Parity bit p1 (position 1)

0

p1 = d1 XOR d2 XOR d4 (covers positions 1,3,5,7)

0 Parity bit p2 (position 2)
0 Parity bit p4 (position 4)
The working Every figure verified twice
  1. p1 = (0 + 0 + 0) mod 2 = 0
  2. p2 = (0 + 0 + 0) mod 2 = 0
  3. p4 = (0 + 0 + 0) mod 2 = 0
Worksheet log
  1. No entries yet — change an input to log a scenario.

How this instrument works

Hamming(7,4) is the classic error-correcting code that takes 4 data bits and adds 3 parity bits, producing a 7-bit codeword that can survive a single flipped bit with no data loss. That's a step beyond simple parity, which only tells you an error happened somewhere — Hamming code tells you exactly which bit flipped, so the receiver can flip it back and recover the original 4 bits without ever asking for a retransmission.

The trick is where the parity bits sit and what each one covers. In the 7-bit codeword, positions 1, 2, and 4 (the powers of two) hold parity bits p1, p2, p4; positions 3, 5, 6, and 7 hold the data bits d1, d2, d3, d4. Each parity bit covers a specific overlapping subset of positions: p1 covers every position whose binary index has a 1 in the ones place (1,3,5,7), p2 covers positions with a 1 in the twos place (2,3,6,7), and p4 covers positions with a 1 in the fours place (4,5,6,7). Because every position belongs to a unique combination of these three groups, a single flipped bit disturbs a unique combination of the three parity checks — and that combination, read as a binary number, is exactly the position of the error.

Richard Hamming invented this scheme in 1950 at Bell Labs, originally to fix a practical annoyance: the relay-based computer he used ran unattended over weekends, and a single punched-card reader error would halt the whole job until Monday. Hamming(7,4) let the machine correct single-bit errors automatically instead of just detecting them and stopping. The same core idea, extended with more parity bits, still underlies the ECC (error-correcting code) memory used in servers today.

This instrument computes the encoding side: enter any 4-bit data word and read off the three parity bits you'd need to build the full 7-bit codeword. It doesn't simulate a transmission error or decode a received codeword for you — the decoding logic, recomputing the checks and reading the resulting syndrome, is explained below, but working it through for a specific received codeword is left as an exercise.

p1=d1d2d4p_1 = d_1 \oplus d_2 \oplus d_4p2=d1d3d4p_2 = d_1 \oplus d_3 \oplus d_4p4=d2d3d4p_4 = d_2 \oplus d_3 \oplus d_4
d1..d4 — the 4 data bits, placed at codeword positions 3, 5, 6, 7 · p1, p2, p4 — parity bits at positions 1, 2, 4, each the XOR (sum mod 2) of a distinct overlapping subset of the data bits, so a single flipped bit uniquely identifies its own position.
  • Toggle Data bit d1 through d4 to 0 or 1 to set the 4-bit data word you want to encode.
  • Read Parity bit p1, p2, and p4 — the three bits Hamming(7,4) computes from your data.
  • Assemble the full 7-bit codeword in position order: p1, p2, d1, p4, d2, d3, d4.
  • To check a received codeword for errors, recompute p1, p2, p4 from its data bits and compare to the parity bits it arrived with — any mismatch pattern points to the flipped position.
  • Try the all-zero and all-one data words first — both are useful sanity checks since every parity bit comes out identical for each.

Worked example — encoding the data word 1011

Set d1=1, d2=0, d3=1, d4=1. p1 = (d1+d2+d4) mod 2 = (1+0+1) mod 2 = 0. p2 = (d1+d3+d4) mod 2 = (1+1+1) mod 2 = 1. p4 = (d2+d3+d4) mod 2 = (0+1+1) mod 2 = 0. Reading off the outputs: p1=0, p2=1, p4=0.

Assembling the 7-bit codeword in position order (p1, p2, d1, p4, d2, d3, d4) gives 0 1 1 0 0 1 1. If a transmission error flipped position 3 (d1, changing it from 1 to 0), the receiver would recompute p1'=(0+0+1)=1 and p2'=(0+1+1)=0, both disagreeing with the received p1=0 and p2=1, while p4' still agrees. That disagreement pattern — p1 and p2 wrong, p4 right — reads as 1+2+0=3 in binary, pointing straight at position 3 as the flipped bit, exactly where the error was introduced.

Questions

What is a Hamming(7,4) code used for?

It protects a 4-bit block of data against a single-bit error by adding 3 parity bits, producing a 7-bit codeword. If exactly one bit flips anywhere in those 7 bits during storage or transmission, the receiver can not only detect that an error happened but also identify and correct the exact bit that flipped, recovering the original data without retransmission. It's the textbook introduction to error-correcting codes, and a simplified ancestor of the ECC memory used in servers and spacecraft today.

How is Hamming code different from a simple parity bit?

A single parity bit can only tell you that an odd number of bits flipped somewhere in a block — it can't say which bit, and it's fooled entirely by an even number of flips. Hamming(7,4) uses three overlapping parity bits instead of one, each covering a different subset of the 7 positions. The specific combination of which checks fail and which pass, read as a binary number, points directly at the flipped position, so the code can correct the error rather than just flag it.

How do you decode a received codeword and correct an error?

Recompute all three parity checks from the 7 received bits the same way they were built, then compare each to what the codeword claims. Combine the three results (0 for match, 1 for mismatch) into a 3-bit number called the syndrome, with the p4-check as the most significant bit and the p1-check as the least significant: a syndrome of 0 means no error, and any nonzero syndrome equals the exact 1-indexed position of the single flipped bit, which you then flip back.

Why are the parity bits at positions 1, 2, and 4 instead of the front?

Positions 1, 2, and 4 are the powers of two within the 7-bit codeword, and every position from 1 to 7 has a unique binary representation using those three bit-place values. Placing the parity bits there means the syndrome — the pattern of which checks fail — is automatically the binary encoding of the error position, with no extra lookup table needed. Placing them anywhere else would break that direct correspondence.

Can Hamming(7,4) correct two bit errors at once?

No — it's designed to correct exactly one bit error per 7-bit codeword. If two bits flip simultaneously, the syndrome still points to some position, but decoding will "correct" the wrong bit and silently produce incorrect data rather than flagging the problem. Extended Hamming codes add one more overall parity bit specifically to at least detect, though still not correct, that a double-bit error occurred — a scheme known as SECDED, single error correction, double error detection.

References