⚙️ Hamming Codes
Visualise how Hamming codes detect and correct single-bit errors. The foundation of modern data storage and transmission.
About Hamming Codes SECDED
Hamming codes are a family of linear error-correcting codes invented by Richard Hamming at Bell Labs in 1950. A Hamming(7,4) code encodes 4 data bits into 7 codeword bits by adding 3 parity bits placed at power-of-two positions (1, 2, 4); each parity bit covers a specific subset of data bit positions defined by the columns of the parity-check matrix H. This arrangement gives the code a minimum Hamming distance of 3, guaranteeing it can correct any single-bit error and detect any two-bit error. Adding one overall parity bit produces the SECDED (Single-Error Correcting, Double-Error Detecting) Hamming(8,4) code used in ECC RAM, where a flipped memory bit is automatically corrected each time the memory is read.
The simulator lets you enter 4-bit data words, watch the parity-check matrix encode them into 7- or 8-bit codewords, inject single or double bit flips, and observe syndrome decoding: the syndrome vector s = H·r (mod 2) directly points to the position of the erroneous bit, allowing automatic correction in one step.
Frequently Asked Questions
How does syndrome decoding locate a single-bit error?
After receiving a codeword r, the decoder computes the syndrome s = H·r (mod 2), where H is the parity-check matrix. If s = 0, no error is detected. If s ≠ 0, it equals one of the columns of H; the position of that column identifies the bit to flip. For Hamming(7,4), the syndrome is a 3-bit binary number directly equal to the 1-based index of the erroneous bit, making correction trivially fast in hardware.
What is the Hamming distance and why is it important?
The Hamming distance between two codewords is the number of bit positions in which they differ. A code with minimum Hamming distance d_min can detect up to d_min − 1 errors and correct up to ⌊(d_min − 1)/2⌋ errors. Hamming(7,4) has d_min = 3, so it corrects 1 error and detects 2. To correct t errors you need d_min ≥ 2t + 1; the minimum codeword length for this grows as O(t log n) by the Hamming bound (sphere-packing bound).
What is ECC RAM and how does it use Hamming codes?
Error-Correcting Code RAM uses a Hamming SECDED scheme on each 64-bit memory word, adding 8 check bits (72 bits stored per word). Each time a word is read, the syndrome is computed in hardware; a single-bit error is corrected transparently in one memory cycle (~60 ns on DDR5) with no software involvement. ECC RAM is standard in servers and workstations where silent data corruption could compromise financial, medical, or scientific computations.
Why are parity bits placed at power-of-two positions?
In Hamming's construction, parity bit at position 2ᵏ covers all bit positions whose binary representation has a 1 in the kth bit. For example, parity bit 1 (position 001₂) covers positions 1, 3, 5, 7 (all positions with bit 0 set). This ensures each data position is covered by a unique non-empty subset of parity bits, so the syndrome uniquely identifies any single-error position. The power-of-two placement makes this subset structure elegant and efficient.
What is the difference between Hamming(7,4) and Hamming(8,4) SECDED?
Hamming(7,4) uses 3 parity bits over 4 data bits and corrects 1 error (d_min = 3). Adding a fourth overall parity bit (XOR of all 7 bits) creates Hamming(8,4), which detects double errors by noticing when the syndrome is non-zero but the overall parity checks out, signalling a 2-bit error pattern. This SECDED property is crucial for memory systems where two errors are rare but possible during cosmic-ray events or multi-cell upsets.
What are Reed-Solomon codes and how do they compare?
Reed-Solomon codes operate over larger alphabets (symbols of k bits rather than single bits) and can correct multiple symbol errors. A Reed-Solomon(255,223) code (used in CDs, DVDs, QR codes, and deep-space communications) corrects up to 16 symbol errors per 255-symbol block. While Hamming codes are optimal for correcting single-bit errors with minimum redundancy, Reed-Solomon is far more powerful for burst errors and is preferred in storage and satellite communication.
What is the Hamming bound (sphere-packing bound)?
The Hamming bound states that for a binary code of length n correcting t errors, the number of codewords M satisfies M · Σᵢ₌₀ᵗ C(n,i) ≤ 2ⁿ. Codes achieving equality are called perfect codes; Hamming codes are perfect (their error spheres of radius 1 partition the entire n-bit space with no gaps). The only other binary perfect codes are the trivial repetition code and the Golay(23,12) code.
How are Hamming codes used beyond RAM?
Hamming codes appear in satellite telemetry (protecting command uplinks), NAND flash ECC (though BCH codes are more common for multi-bit correction), early data transmission modems, magnetic tape, and network protocol headers. Hamming(7,4) is also a classic teaching example in information theory and algebraic coding theory courses, illustrating the power of linear algebra over GF(2).
What happens if there are two simultaneous bit errors in Hamming(7,4)?
With only single-error correction (no overall parity bit), a 2-bit error produces a non-zero syndrome that matches some column of H — but the wrong one. The decoder "corrects" the wrong bit, introducing a third error and corrupting the data silently. SECDED (8,4) prevents this: the overall parity bit distinguishes 1-bit errors (odd parity mismatch + non-zero syndrome) from 2-bit errors (even parity mismatch + non-zero syndrome), flagging the latter rather than miscorrecting.
Frequently Asked Questions
What is a Hamming code?
How does syndrome decoding work?
What is SECDED and why does it matter?
Where are Hamming codes used in practice?
Why are parity bits placed at power-of-2 positions?
What is the Hamming distance?
What is the code rate of Hamming(7,4)?
Can Hamming codes correct more than one bit error?
How is the parity-check matrix H constructed?
What is the difference between error detection and error correction?
Flip a bit in a Hamming-encoded word and watch the parity checks produce a syndrome that points straight at the error's position.
2D · HTML5 Canvas 2D · 60 FPS target · runs fully client-side, no install