0
Explore
0

Hamming Code: Concept and Working with a Detailed Example

Updated on November 10, 2025

Introduction

Data sent over a network, stored in memory, or recorded on a disk can get corrupted by noise, interference, or hardware faults. Even a single incorrect bit can change the meaning of data and cause serious problems. To deal with this, engineers use error detection and correction techniques. One of the earliest and most elegant techniques is the Hamming code, developed by Richard W. Hamming in 1950.

Hamming code is simple, fast, and practical. It can detect and correct any single-bit error in a block of data. With a small overhead, it allows systems to find where the error happened and fix it on the fly. Hamming codes are widely used in computer memory (ECC memory), satellites, and communication systems.

Definition and Basic Idea

A Hamming code is a linear block error-correcting code that adds carefully chosen parity bits (check bits) to a block of data bits. These parity bits are placed at specific positions so that, when an error occurs, the parity checks produce a unique pattern (called a syndrome) that identifies the exact bit position that flipped. The receiver can then correct the error by flipping that bit back.

The simplest and most common Hamming code is the (7,4) code. Here:

  • 7 = total number of bits in the codeword (data + parity)
  • 4 = number of data bits
  • 3 = number of parity bits (7 − 4 = 3)

In general, for a Hamming code with r parity bits, the total codeword length is n = 2^r − 1 and the number of data bits is k = n − r.

Why Error Control Coding Is Needed

During transmission or storage, bits may flip randomly due to noise or physical defects. Without protection, the receiver cannot tell whether the data is correct. Basic parity (a single parity bit for a block) can detect an odd number of bit errors but cannot correct them. Hamming code goes further: it can locate and correct one-bit errors automatically and detect, though not correct, two-bit errors when extended with one extra parity bit (SECDED: Single Error Correction, Double Error Detection).

Key Concepts and Terminology

  • Codeword: The full block of bits sent, including data bits and parity bits.
  • n, k, r: n = codeword length, k = number of data bits, r = number of parity bits, with n = k + r.
  • Hamming distance: The number of bit positions in which two codewords differ. A Hamming code has minimum distance 3, which is why it can correct 1-bit errors (distance 2 is needed to detect single-bit errors, and 3 is needed to correct them).
  • Parity: Even parity means the count of 1s in the chosen set is even. Odd parity means the count is odd. Hamming codes usually use even parity.
  • Syndrome: The pattern formed by re-evaluating the parity checks at the receiver. It is a binary number that directly points to the position of the error (if exactly one bit is wrong).

How Many Parity Bits Are Needed?

Hamming derived a simple rule to find the minimum number of parity bits r needed to protect m data bits (so k = m):

2r ≥ m + r + 1

Reasoning: with r parity bits, there are 2r possible syndrome patterns. One pattern indicates “no error.” The remaining (2r − 1) patterns must uniquely identify which one of the (m + r) bit positions is wrong. So we need 2r − 1 ≥ m + r or 2r ≥ m + r + 1.

Example: Suppose m = 4 data bits. Try r = 3. Then 23 = 8 and m + r + 1 = 4 + 3 + 1 = 8. The inequality holds, so r = 3 is enough. Thus n = m + r = 7, which is the (7,4) Hamming code.

Positioning of Parity Bits

In a Hamming code, the parity bits are placed at positions that are powers of two: 1, 2, 4, 8, 16, and so on. The remaining positions carry data bits. For (7,4), the parity bits are at positions 1, 2, and 4; the data bits occupy positions 3, 5, 6, and 7.

Each parity bit checks a specific set of positions. The rule is based on the binary representation of the position numbers:

  • Parity bit at position 1 (binary 001) checks all positions whose least significant bit is 1: positions 1, 3, 5, 7.
  • Parity bit at position 2 (binary 010) checks positions 2, 3, 6, 7 (where the second bit is 1).
  • Parity bit at position 4 (binary 100) checks positions 4, 5, 6, 7 (where the third bit is 1).

Text diagram for (7,4) positions:

[1:p1] [2:p2] [3:d1] [4:p4] [5:d2] [6:d3] [7:d4]

Coverage sets under even parity:

  • p1 checks positions 1,3,5,7
  • p2 checks positions 2,3,6,7
  • p4 checks positions 4,5,6,7

These coverage sets ensure that each bit position has a unique “signature” of parity bits that check it. That uniqueness is what makes correction possible.

Encoding Procedure (Step-by-Step)

We now encode a 4-bit message using the (7,4) Hamming code with even parity. Suppose the data bits are 1 0 1 1. We will place them at positions 3, 5, 6, and 7 in this order.

Step 1: Place the data bits in their positions:

Positions: 1 2 3 4 5 6 7
Bits: ? ? 1 ? 0 1 1

Here, “?” are parity bits p1 (position 1), p2 (position 2), and p4 (position 4).

Step 2: Compute parity bit p1 (position 1) covering positions 1, 3, 5, 7:

  • Current bits at 3, 5, 7 are 1, 0, 1 respectively; sum = 1 + 0 + 1 = 2 (even)
  • To keep overall parity even, p1 must be 0.

Step 3: Compute parity bit p2 (position 2), covering positions 2, 3, 6, 7:

  • Current bits at 3, 6, 7 are 1, 1, 1; sum = 3 (odd)
  • To make the total even, p2 must be 1.

Step 4: Compute parity bit p4 (position 4), covering positions 4, 5, 6, 7:

  • Current bits at 5, 6, 7 are 0, 1, 1; sum = 2 (even)
  • To keep parity even, p4 must be 0.

Step 5: Write the final codeword:

Positions: 1 2 3 4 5 6 7
Bits: 0 1 1 0 0 1 1

So the encoded (7,4) codeword is 0 1 1 0 0 1 1.

Decoding and Error Correction (Step-by-Step)

At the receiver, we recompute the parity checks to find out whether there was an error and where it occurred. The checking results form the syndrome. With even parity:

  • s1 is the parity over positions 1, 3, 5, 7 (should be even)
  • s2 is the parity over positions 2, 3, 6, 7 (should be even)
  • s4 is the parity over positions 4, 5, 6, 7 (should be even)

Interpret the syndrome bits as a binary number (s4 s2 s1). If this binary number is zero, there is no error. If it is non-zero, it gives the position of the erroneous bit. We then flip that bit to correct the codeword.

Worked Example 1: Single-Bit Error and Correction

Start with the correct codeword: 0 1 1 0 0 1 1. Suppose a noise burst flips the bit at position 5 during transmission. The received vector is:

Received: 0 1 1 0 1 1 1

Now compute the parity checks:

  • s1 over positions 1, 3, 5, 7: bits 0, 1, 1, 1 → sum = 3 (odd) → s1 = 1 (parity check fails)
  • s2 over positions 2, 3, 6, 7: bits 1, 1, 1, 1 → sum = 4 (even) → s2 = 0 (parity check passes)
  • s4 over positions 4, 5, 6, 7: bits 0, 1, 1, 1 → sum = 3 (odd) → s4 = 1 (parity check fails)

Syndrome (s4 s2 s1) = 1 0 1, which is binary 5. This indicates an error at position 5. Flip that bit:

Corrected: 0 1 1 0 0 1 1 (back to the original codeword). Extract the data bits at positions 3, 5, 6, 7→ 1011

Observation: The decoding process requires only simple parity checks and one bit flip, making it efficient for hardware or firmware implementation.

Parity-Check Matrix and Syndrome

Hamming codes are linear block codes, so they can also be described using matrices. For the (7,4) code, the parity-check matrix H is a 3 × 7 matrix whose columns are the binary representations of the numbers 1 to 7. One common arrangement (with rows corresponding to s1, s2, s4) is:

H =
[ 1 0 1 0 1 0 1 ] ← checks for p1 (positions with least significant bit 1)
[ 0 1 1 0 0 1 1 ] ← checks for p2
[ 0 0 0 1 1 1 1 ] ← checks for p4

Given a received vector r of length 7, the syndrome s is computed as s = H × r^T over modulo 2 arithmetic (XOR). The resulting 3-bit vector s corresponds to (s1, s2, s4). Its binary value is the position index of the erroneous bit if there is exactly one error. This matrix viewpoint is useful in coding theory and helps generalize to longer Hamming codes (e.g., (15,11), (31,26)).

A generator matrix G (size 4 × 7 for (7,4)) maps data vectors to codewords via c = u × G, where u is a 4-bit data vector. While matrix encoding is less common in hand calculations, it is standard in software and hardware implementations.

Worked Example 2: Parity Bit Error

Consider the same original codeword 0 1 1 0 0 1 1, but now suppose the channel flips parity bit at position 2. The received vector is:

Received: 0 0 1 0 0 1 1

Compute checks:

  • s1 over positions 1, 3, 5, 7: bits 0, 1, 0, 1 → sum = 2 (even) → s1 = 0
  • s2 over positions 2, 3, 6, 7: bits 0, 1, 1, 1 → sum = 3 (odd) → s2 = 1
  • s4 over positions 4, 5, 6, 7: bits 0, 0, 1, 1 → sum = 2 (even) → s4 = 0

Syndrome (s4 s2 s1) = 0 1 0 = binary 2. This identifies position 2 as the error. Flip bit 2 to correct, and the codeword is restored.

What About Two-Bit Errors?

A basic Hamming code has minimum distance 3, so it can correct only one error. With two errors, the syndrome will generally point to the wrong position, causing a miscorrection. For example, if positions 5 and 6 are both flipped in our (7,4) example, the syndrome may indicate position 3 (a data bit that was not wrong), and the attempted “correction” will create a third error.

That is why in safety-critical systems we often use the extended Hamming code (SECDED), which adds one additional overall parity bit. This allows the system to detect (but not correct) double-bit errors and avoid applying a wrong correction.

Extended Hamming Code (SECDED)

The extended Hamming code adds one more parity bit p0 that covers the entire codeword (all bits including the original parity bits). The total number of bits becomes n + 1. The rules for interpreting the overall parity bit and the syndrome are:

  • If the syndrome is 000 and the overall parity check passes (even), then no error occurred.
  • If the syndrome is non-zero and the overall parity check fails (odd), then there is a single-bit error at the position indicated by the syndrome. Correct it.
  • If the syndrome is non-zero but the overall parity check passes (even), then a double-bit error is likely. Do not correct; raise an error.
  • If the syndrome is 000 but the overall parity check fails (odd), then the error is in the overall parity bit p0. Flip p0.

With one additional bit, the extended Hamming code achieves minimum distance 4, improving detection capability without changing the single-error-correction property.

Generalization to Larger Hamming Codes

You can construct longer Hamming codes by increasing r, which increases n = 2r − 1 and k = n − r. Examples:

  • r = 3 → (7,4)
  • r = 4 → (15,11)
  • r = 5 → (31,26)

The parity bits are at positions 1, 2, 4, 8, 16, and the coverage rule is the same: each parity bit checks all positions whose binary representation has a 1 in the corresponding bit place. Matrix H remains a set of columns equal to the binary representations of 1..n.

Advantages of Hamming Codes

  • Single-error correction with low overhead: Only r parity bits are needed, where 2^r ≥ m + r + 1.
  • Fast and hardware-friendly: Parity checks use XOR operations, which are simple and fast in digital circuits.
  • Deterministic correction: The syndrome directly points to the error position.
  • Extendable to SECDED: Adding an overall parity bit enables detection of double-bit errors.

Limitations

  • Corrects only one-bit errors: Standard Hamming codes cannot reliably handle two-bit errors and will miscorrect.
  • Limited burst-error handling: If many consecutive bits are corrupted, Hamming code is not sufficient; stronger codes like Reed-Solomon or convolutional codes may be needed.
  • Overhead grows with protection: As data blocks get larger, you must add more parity bits to maintain single-error-correction capability.

Real-World Applications

  • ECC memory (Error Correcting Code memory): Many servers and workstations use SECDED Hamming codes to correct single-bit errors in RAM and detect double-bit errors, improving reliability.
  • Communication links: Hamming codes are used in low-latency links where simple, fast error correction is needed (e.g., telemetry, simple sensor networks).
  • Storage devices: Older disk systems and some flash controllers have used Hamming-style codes for basic protection, though modern systems often use stronger codes.
  • Embedded systems: Microcontrollers with limited resources can implement Hamming codes efficiently for robust data transfer across buses and interfaces.

Common Mistakes and Tips

  • Wrong parity convention: Always be consistent. If you choose even parity, keep it for all parity bits and checks.
  • Incorrect coverage sets: Remember that parity bit at position 2^i checks all positions whose binary index has a 1 in bit i. For (7,4): p1 → 1,3,5,7; p2 → 2,3,6,7; p4 → 4,5,6,7.
  • Forgetting to interpret syndrome correctly: The order of syndrome bits matters. If you define syndrome as (s4 s2 s1) in that order, interpret it as a binary number to get the error position.
  • Extracting data bits: After correcting, read data from the non-parity positions only (for (7,4): 3, 5, 6, 7).
  • Double-error miscorrection: Basic Hamming codes may miscorrect double errors. In critical systems, use extended Hamming (SECDED) to detect double errors and avoid wrong corrections.

Step-by-Step Summary Algorithm

Encoding (with even parity):

  1. Choose r so that 2r ≥ m + r + 1, where m is the number of data bits.
  2. Build a codeword of length n = m + r. Put parity bits at positions 1, 2, 4, 8, …; fill the remaining positions with data bits.
  3. For each parity bit p at position 2^i, compute the parity over all positions whose binary representation has a 1 in bit i, including p itself. Set p so that the total is even.
  4. Transmit the codeword.

Decoding and correction:

  1. Receive the n-bit word (or n+1 for SECDED).
  2. Recompute the parity checks for p1, p2, p4, … to form the syndrome s = (s4 s2 s1 …).
  3. If using SECDED, also compute the overall parity of the entire word (including p0 if present).
  4. If syndrome is zero and overall parity (if used) is correct, accept the word.
  5. If syndrome is non-zero and overall parity (if used) indicates an odd error count, flip the bit at the position indicated by the syndrome.
  6. If using SECDED and syndrome is non-zero but overall parity indicates even, flag as double error (do not correct).
  7. Extract the data bits from non-parity positions.

Another Brief Example: (15,11) Hamming Code Structure

For r = 4, n = 15, k = 11. Parity bits are at positions 1, 2, 4, and 8. Data bits occupy the remaining positions (3, 5–7, 9–15). The parity-check matrix H is 4 × 15, with columns equal to the 4-bit binary representations of 1 through 15. The decoding process is the same: compute s1, s2, s4, s8, form the 4-bit syndrome, and correct the indicated position for any single-bit error.

Example

Imagine a row of boxes labeled 1 to 7. Boxes 1, 2, and 4 hold parity bits. Boxes 3, 5, 6, and 7 hold data bits. Draw lines from:

  • Box 1 to boxes 1, 3, 5, 7
  • Box 2 to boxes 2, 3, 6, 7
  • Box 4 to boxes 4, 5, 6, 7

Each parity box “looks at” the boxes it connects to and ensures an even number of 1s. At the receiver, redrawing the lines and checking parity again tells you exactly which box (if any) is wrong.

Putting It All Together: Why Hamming Code Matters

Hamming codes elegantly solve the problem of correcting single-bit errors with minimal overhead. They provide:

  • A formula to determine how many parity bits are required (2^r ≥ m + r + 1)
  • A systematic placement of parity bits at power-of-two positions
  • Simple XOR-based parity calculations for encoding and decoding
  • A syndrome whose binary value directly identifies the error position
  • An easy extension (SECDED) to detect double-bit errors safely

Because they are simple and effective, Hamming codes remain a fundamental topic in digital communication and computer architecture. They are often the first practical error-correcting codes that students learn, and the ideas behind them (distance, parity checks, syndromes) generalize to more powerful modern codes.

Conclusion

Hamming code is a classic and powerful error-correcting approach that uses a small number of parity bits to ensure robust communication and storage. Its core principles are straightforward: place parity bits at positions that are powers of two, define coverage sets based on binary indices, compute parity for evenness, and use the resulting syndrome to locate and correct any single-bit error. Through a step-by-step example with the (7,4) code, we saw how to encode a 4-bit message, how a single-bit error changes the received word, and how the syndrome reveals the exact error position.

While basic Hamming codes cannot reliably handle two-bit errors, the extended version (SECDED) adds an overall parity bit to detect double errors and avoid miscorrection. These features make Hamming codes a practical choice in systems where single-bit errors are the most common and low-latency correction is crucial, such as ECC memory and real-time communication links. Understanding Hamming codes not only equips you with a useful tool but also builds intuition for more advanced error-control coding techniques used in modern digital systems.