Hamming Code: Error Detection and Correction with Examples

โšก Smart Summary

Hamming code is a linear error-correcting code that adds redundant parity bits at power-of-two positions, allowing a receiver to detect up to two-bit errors and automatically correct any single-bit error during data transmission.

  • ๐Ÿงญ Purpose: Hamming code detects and corrects transmission errors by embedding parity bits that pinpoint the exact position of a corrupted bit.
  • ๐Ÿ”ข Redundant bits: The number of parity bits (p) satisfies the rule 2^p โ‰ฅ n + p + 1, where n is the number of data bits.
  • ๐Ÿ“ Placement: Parity bits occupy power-of-two positions โ€” 1, 2, 4, and 8 โ€” while data bits fill the remaining positions.
  • ๐Ÿงฎ Hamming(7,4): A common form encodes four data bits into seven total bits using three parity bits.
  • ๐Ÿงฏ Limitation: Standard Hamming code corrects only single-bit errors; an extra overall parity bit adds double-error detection (SECDED).
  • ๐Ÿค– AI assistance: Machine-learning decoders help flag noisy channels and error patterns that fixed codes alone may miss.

Hamming code error detection and correction with parity bits

What is an Error?

Transmitted data can be corrupted during communication. It is likely to be affected by external noise or other physical failures. In such a situation, the input data cannot be the same as the output data. This mismatch is known as an “Error.”

Data errors may cause the loss of important or secure data. Most of the data transfer in digital systems takes the form of “bit transfer,” and even a small change of a single bit can affect the performance of the entire system. In a data sequence, if a 1 is changed to a 0, or a 0 is changed to a 1, it is called a “bit error.”

Types of Errors

There are mainly three types of bit error that occur when data is transmitted from the sender to the receiver. The diagram below illustrates how each type affects a data sequence:

Diagram comparing single-bit, multiple-bit, and burst errors in a binary data sequence

  • Single bit errors
  • Multiple bit errors
  • Burst errors

Single Bit Errors

A change made to one bit in the entire data sequence is known as a “single-bit error.” The occurrence of a single-bit error is not that common. It mostly occurs in a parallel communication system, because data is transferred bit-wise on separate lines, so there is a higher chance that one line becomes noisy while the others stay clean.

Multiple Bit Errors

When two or more bits of a data sequence change between the transmitter and the receiver, it is known as a “multiple-bit error.”

This type of error occurs in both serial and parallel data communication networks.

Burst Errors

A change to a set of consecutive bits in a data sequence is known as a “burst error.” The length of a burst error is measured from the first changed bit to the last changed bit.

What is Error Detection and Error Correction?

In a digital communication system, errors can be introduced as data moves from one device to another. If these errors are not detected and corrected, the data is lost. For effective communication, data must be transferred with high accuracy, which is achieved by first identifying the errors and then correcting them.

Error detection is a method of finding the errors present in the data transmitted from a transmitter to a receiver in a data communication system.

Redundancy codes are used to find these errors by adding extra bits to the data when it is transmitted from the source. These extra bits are called “error-detecting codes.” The three most common types of error-detection code are:

  • Parity Checking
  • Cyclic Redundancy Check (CRC)
  • Longitudinal Redundancy Check (LRC)

Parity Checking

  • It is also known as a parity check.
  • It provides a cost-effective mechanism for error detection.
  • In this technique, the redundant bit added to every data unit is known as a parity bit. It is set so that the total number of 1s in the unit becomes even (even parity) or odd (odd parity).

Longitudinal Redundancy Check

In this error-detection technique, a block of bits is organized in a table. The LRC method calculates a parity bit for every column, and this set of parity bits is sent along with the original data. The block of parity bits helps the receiver check for redundancy and detect errors.

Cyclic Redundancy Check

Cyclic Redundancy Check appends a sequence of redundant bits to the end of the data unit so that the resulting data unit becomes exactly divisible by a second, predetermined binary number.

At the destination, the incoming data is divided by the same number. If there is no remainder, the data unit is assumed to be correct and is accepted. Otherwise, it indicates that the data unit was damaged in transmission, and it must be rejected.

What is a Hamming Code?

Hamming code is a linear code that is useful for detecting up to two immediate bit errors and correcting single-bit errors. Error correction of this kind typically operates at the data link layer, framing data and checking its integrity between adjacent nodes.

In Hamming code, the source encodes the message by adding redundant bits. These redundant bits are inserted and generated at specific positions in the message to carry out the error detection and correction process.

History of Hamming Code

  • Hamming code is a technique built by R. W. Hamming to detect and correct errors.
  • It can be applied to data units of any length and uses the relationship between data bits and redundancy bits.
  • Hamming worked on the problem of error correction and developed an increasingly powerful array of algorithms.
  • In 1950, he published the Hamming code, which is still widely used today in applications such as ECC memory.

Applications of Hamming Code

Here are some common applications of Hamming code:

  • Satellites
  • Computer memory (ECC RAM)
  • Modems
  • PlasmaCAM
  • Open connectors
  • Shielded wire
  • Embedded processors

Advantages of Hamming Code

  • Hamming code is effective on networks where data streams are exposed to single-bit errors.
  • It not only detects a bit error but also helps you identify the bit that contains the error so that it can be corrected.
  • The ease of use of Hamming codes makes them well suited for computer memory and single-error correction.

Disadvantages of Hamming Code

  • It is a single-bit error detection and correction code. If multiple bits are found to be in error, the outcome may flip another bit that was actually correct, corrupting the data further.
  • The Hamming code algorithm can resolve only single-bit issues.

How to Encode a Message in Hamming Code

The process the sender uses to encode the message involves the following three steps:

  • Calculate the total number of redundant bits.
  • Determine the position of the redundant bits.
  • Calculate the value of each redundant bit.

When the redundant bits are embedded within the message, the complete codeword is sent to the receiver.

Step 1) Calculate the total number of redundant bits.

Assume that the message contains n data bits and p redundant bits, added so that 2p can indicate at least (n + p + 1) different states.

Here, (n + p) accounts for the location of an error in each of the (n + p) bit positions, and one extra state indicates no error. Because p parity bits can indicate 2p states, 2p must be at least equal to (n + p + 1).

Step 2) Place the redundant bits in their correct positions.

The p redundant bits are placed at bit positions that are powers of 2 โ€” for example, 1, 2, 4, 8, and 16. They are referred to as p1 (at position 1), p2 (at position 2), p3 (at position 4), and so on.

Step 3) Calculate the value of each redundant bit.

Each redundant bit is a parity bit that makes the number of 1s in its group either even or odd. The two types of parity are:

  • Even parity: the total number of 1s in the covered positions is made even.
  • Odd parity: the total number of 1s in the covered positions is made odd.

Each parity bit covers a specific set of positions, determined by the binary representation of the position numbers:

  • p1 checks every position whose binary value has a 1 in the least significant bit โ€” positions 1, 3, 5, 7, 9, 11, and so on.
  • p2 checks every position whose binary value has a 1 in the second bit from the right โ€” positions 2, 3, 6, 7, 10, 11, and so on.
  • p3 checks every position whose binary value has a 1 in the third bit from the right โ€” positions 4 to 7, 12 to 15, and so on.

Worked example (7,4): Consider the four data bits 1011. Three parity bits are required (23 = 8 โ‰ฅ 4 + 3 + 1), giving a seven-bit codeword laid out as p1 p2 d1 p3 d2 d3 d4. Placing the data puts positions 3, 5, 6, 7 = 1, 0, 1, 1. Using even parity: p1 covers positions 1, 3, 5, 7 (bits 1, 0, 1 โ†’ p1 = 0); p2 covers 2, 3, 6, 7 (bits 1, 1, 1 โ†’ p2 = 1); p3 covers 4, 5, 6, 7 (bits 0, 1, 1 โ†’ p3 = 0). The transmitted codeword is therefore 0110011.

How to Decode a Message in Hamming Code

The receiver takes the incoming message and performs recalculations to find and correct errors. The recalculation process uses the following steps:

  • Count the number of redundant bits.
  • Correctly position all the redundant bits.
  • Perform the parity check.

Step 1) Count the number of redundant bits. Use the same formula as for encoding: 2p โ‰ฅ n + p + 1, where n is the number of data bits and p is the number of redundant bits.

Step 2) Correctly position all the redundant bits. Each redundant bit sits at a bit position that is a power of 2 โ€” for example, 1, 2, 4, and 8.

Step 3) Perform the parity check. The parity bits are recalculated from the data bits and the received redundant bits:

  • p1 = parity(1, 3, 5, 7, 9, 11, โ€ฆ)
  • p2 = parity(2, 3, 6, 7, 10, 11, โ€ฆ)
  • p3 = parity(4โ€“7, 12โ€“15, 20โ€“23, โ€ฆ)

The recalculated parity bits together form a binary number. If that number is zero, no error is present; otherwise, its value gives the exact position of the single corrupted bit, which is then flipped to correct the message.

FAQs

Hamming distance is the number of bit positions in which two equal-length binary strings differ. In error-correcting codes, the minimum Hamming distance between valid codewords determines how many errors can be detected or corrected. Standard Hamming code has a minimum distance of three.

Hamming(7,4) encodes four data bits into a seven-bit codeword by adding three parity bits. It corrects any single-bit error and detects two-bit errors. The notation (n, k) states the total codeword length n and the number of data bits k.

A single parity bit only detects an odd number of bit errors and cannot locate or correct them. Hamming code uses multiple parity bits at power-of-two positions, so it pinpoints the exact failed bit and corrects it automatically.

Basic Hamming code corrects one-bit errors but may miscorrect two-bit errors. Adding one overall parity bit covering the whole codeword creates SECDED (single error correction, double error detection), raising the minimum distance to four and reliably detecting double-bit errors.

Error detection and correction mainly occur at the data link layer of the OSI model, which frames data and checks its integrity between adjacent nodes. Transport-layer protocols add end-to-end checks, while the physical medium introduces the noise these codes guard against.

Hamming code is a linear block code. It processes a fixed-size block of data bits at once and appends parity bits, unlike convolutional codes that encode a continuous bit stream using memory of previous bits. This makes Hamming code simple and fast.

Machine-learning models learn the noise patterns of a channel and predict likely bit errors, improving decoding accuracy beyond fixed schemes. AI-driven decoders for LDPC and polar codes now assist modern 5G and storage systems where classic Hamming code alone is insufficient.

GitHub Copilot can scaffold encoder and decoder functions, generate parity-bit masks, and draft unit tests from a short comment. Verify the bit-position math and parity groupings carefully, because off-by-one indexing is a frequent source of errors in generated code.

Summarize this post with: