What Is Hamming Code? Working, Example, and Applications
TL;DR: Hamming code is an error-detection and error-correction technique that adds redundant (parity) bits to data so a receiver can detect up to 2-bit errors and automatically correct any single-bit error, without retransmission.

Whenever information travels over a network, down a cable, or is stored somewhere, there is a small chance one bit will flip for various reasons. A single corrupted bit can turn an otherwise fine data set into an essentially unreadable mess. Hamming code encodes extra information in bits so that when the receiver detects a corrupted bit, it cannot only notice it but, in most cases, recover the correct data without needing to ask for a resend. This article explains what the Hamming code is, what the extra bits represent, walks through a detailed example of how it works under even and odd parity schemes, compares it to a similar concept called Hamming distance, and covers its uses.

What Is Hamming Code?

The Hamming code is an error-detecting and error-correcting method designed by Richard W. Hamming in 1950 while he was working at Bell Labs. This error correction code works by adding extra parity bits to the data being sent. The Hamming code allows the receiver to check that the data was received correctly and determine whether it has been corrupted. If a bit is corrupted, the Hamming code detects the corrupted bit and corrects it without resending the data.

Hamming code is widely used when retransmission is expensive or impossible, such as in satellite communication, RAM error correction (ECC memory), and embedded systems.

Cloud Architect Master's ProgramExplore Program
Get Certifications in AWS, Azure, and Google Cloud

Important Terms for Hamming Code

  • Data bits (d): The actual information bits being transmitted.
  • Redundant bits/parity bits (r): Extra bits added to the data to enable error detection and correction. The number of redundant bits needed is found using the formula 2^r ≥ d + r + 1.
  • Parity: A method of appending bits so that the total count of 1s in a group is either always even (even parity) or always odd (odd parity).
  • Even parity: The parity bit is set so that the total number of 1s in the group (data bits + parity bit) is even.
  • Odd parity: The parity bit is set so that the total number of 1s in the group is odd.
  • Hamming distance: The number of bit positions in which two binary strings of equal length differ. It is the theoretical basis for how many errors a code can detect and correct.

Hamming Distance vs. Hamming Code

These terms are related, but not the same, and mixing them up is a common source of confusion.

  • Hamming distance is a measure: the number of positions where two equal-length binary strings differ. For example, the Hamming distance between 1011101 and 1001001 is 2, because they differ at exactly two bit positions.
  • Hamming code is a technique: a scheme for adding redundant bits to a message so that all valid codewords are spaced far enough apart, in terms of Hamming distance, that errors can be detected and corrected.

The minimum Hamming distance between any two valid codewords in a code determines what that code can do:

Minimum Hamming Distance

Error Detection

Error Correction

1

None

None

2

Detects 1-bit errors

None

3

Detects up to 2-bit errors

Corrects 1-bit errors

The standard Hamming code is designed to have a minimum Hamming distance of 3 between valid codewords, so it can detect up to 2-bit errors but correct only 1-bit errors. If two bits flip at once, the code can detect an error, but it cannot reliably determine which bits to fix, and it may even correct the codeword to the wrong value.

How Many Errors Can Hamming Code Detect and Correct?

This is one of the most searched questions about Hamming code, and the direct answer is:

  • Detects: up to 2-bit errors
  • Corrects: exactly 1-bit errors

If exactly one bit is flipped during transmission, Hamming code detects the error and identifies the faulty bit's position so it can be automatically corrected. If two bits are flipped, Hamming code can detect an error (the parity bits will not match). Still, it cannot identify which two bits are incorrect, and applying the Hamming error-correction algorithm to a two-bit error will silently fail. This is why Hamming code is best suited to environments where single-bit errors are the likely failure mode (such as memory chips or short-range transmission links) and is not suitable for noisy channels where burst errors are more common.

With Our Azure Cloud Architect Masters ProgramEnroll Now
Build and Deploy Azure Applications Like a Pro!

Working of Hamming Code

Implementing Hamming code follows four steps:

  1. Calculate the number of redundant bits. Using the formula 2^r ≥ d + r + 1, where d is the number of data bits and r is the number of redundant bits, find the smallest r that satisfies the inequality.
  2. Position the redundant bits. Place redundant bits at positions that are powers of 2 (1, 2, 4, 8, 16, ...), counting from the left, position 1. All other positions hold the data bits.
  3. Calculate each parity bit. Each parity bit covers a specific set of positions, determined by the binary representation of the position numbers, and is set to make the count of 1s in its group even (even parity) or odd (odd parity).
  4. Verify and correct at the receiver. The receiver recalculates each parity group. If all checks pass, the data is assumed error-free. If one or more checks fail, the pattern of failures points to the exact position of the flawed bit, which is then flipped to correct it.

How Parity Bit Positions Are Calculated

Each parity bit checks a specific group of bit positions, based on the binary representation of the position index. A position is covered by a given parity bit if that position's binary form has a 1 in the corresponding bit place:

  • P1 (position 1, binary 0001) checks all positions whose binary representation has a 1 in the least significant bit: 1, 3, 5, 7, 9, 11, 13, ...
  • P2 (position 2, binary 0010) checks all positions whose binary representation has a 1 in the second bit: 2, 3, 6, 7, 10, 11, 14, 15, ...
  • P4 (position 4, binary 0100) checks all positions whose binary representation has a 1 in the third bit: 4, 5, 6, 7, 12, 13, 14, 15, ...
  • P8 (position 8, binary 1000) checks all positions whose binary representation has a 1 in the fourth bit: 8, 9, 10, 11, 12, 13, 14, 15, ...

This pattern extends for any additional parity bits (P16, P32, and so on) needed for longer data streams.

Hamming Code Algorithm (Encoding and Decoding)

Encoding (Sender Side):

  1. Determine r using 2^r ≥ d + r + 1.
  2. Number the bit positions from 1 to d + r, left to right.
  3. Place the data bits into the non-power-of-2 positions, in order.
  4. For each parity bit position, count the 1s in its associated group of positions (excluding the parity bit itself) and set the parity bit so the group's total number of 1s matches the chosen parity (even or odd).
  5. Transmit the complete sequence, including data and parity bits.

Decoding (Receiver Side):

  1. Recalculate each parity group exactly as the sender did, this time including the received parity bit in the count.
  2. For each group, record a 0 if the parity still holds and a 1 if it does not.
  3. Read the recorded bits together, from the highest-order parity check to the lowest (for example, C8 C4 C2 C1), as a binary number.
  4. If the result is 0, no error occurred. If the result is non-zero, that number is the position of the flipped bit.
  5. Flip the bit at that position to correct the error.
Develop the technical skills needed to design secure, scalable, and reliable cloud solutions. Enroll in Simplilearn's Cloud Architect Master's Program and take the next step in your cloud career.

Example for Hamming Code (Even Parity)

Consider the 7-bit data 1011010 that needs to be transmitted with error correction. This example uses 7 data bits and, as shown below, 4 redundant bits, making it a form of the well-known Hamming(11,7) code (a larger relative of the commonly cited Hamming(7,4) code, which encodes 4 data bits using 3 parity bits).

Hamming Code Example

Step 1: Calculate Redundant Bits

Using 2^r ≥ d + r + 1 with d = 7:

  • Try r = 4: 2^4 = 16, and d + r + 1 = 7 + 4 + 1 = 12. Since 16 ≥ 12, r = 4 works.

Step 2: Position the Bits

Total length = d + r = 11 bits. Positions 1, 2, 4, and 8 hold the parity bits (P1, P2, P4, P8). The data bits 1011010 are placed into the remaining positions (3, 5, 6, 7, 9, 10, 11) in order, from position 11 down to position 3, skipping the parity positions:

Position

1

2

3

4

5

6

7

8

9

10

11

Bit

P1

P2

0

P4

1

0

1

P8

1

0

1

Hamming Code Example

Hamming Code Example

Step 3: Calculate Each Parity Bit (Even Parity)

  • P1 covers 1, 3, 5, 7, 9, 11 → data values at 3, 5, 7, 9, 11 = 0, 1, 1, 1, 1 → four 1s (even) → P1 = 0
  • P2 covers 2, 3, 6, 7, 10, 11 → data values at 3, 6, 7, 10, 11 = 0, 0, 1, 0, 1 → two 1s (even) → P2 = 0
  • P4 covers 4, 5, 6, 7 → data values at 5, 6, 7 = 1, 0, 1 → two 1s (even) → P4 = 0
  • P8 covers 8, 9, 10, 11 → data values at 9, 10, 11 = 1, 0, 1 → two 1s (even) → P8 = 0

The full transmitted sequence is: 0 0 0 0 1 0 1 0 1 0 1 (positions 1 through 11).

Hamming Code Example

Step 4: Detect and Correct an Error

Suppose the bit at position 7 flips during transmission, from 1 to 0. The receiver has: 0 0 0 0 1 0 0 0 1 0 1.

Hamming Code Example

The receiver recalculates each parity check, including the parity bit itself this time:

  • C1 (positions 1, 3, 5, 7, 9, 11): values 0, 0, 1, 0, 1, 1 → three 1s (odd) → mismatch → C1 = 1
  • C2 (positions 2, 3, 6, 7, 10, 11): values 0, 0, 0, 0, 0, 1 → one 1 (odd) → mismatch → C2 = 1
  • C4 (positions 4, 5, 6, 7): values 0, 1, 0, 0 → one 1 (odd) → mismatch → C4 = 1
  • C8 (positions 8, 9, 10, 11): values 0, 1, 0, 1 → two 1s (even) → match → C8 = 0

Reading the checks as C8 C4 C2 C1 gives 0111, which is 7 in binary. This tells the receiver the error is at position 7, exactly matching the assumed error. The receiver flips the bit at position 7 back from 0 to 1, fully restoring the original transmitted sequence and, with it, the original data 1011010.

Example for Hamming Code (Odd Parity)

The process is identical under odd parity, except each parity bit is set so its group has an odd number of 1s instead of an even one.

Using the same 7-bit data 1011010 placed into the same positions:

Position

1

2

3

4

5

6

7

8

9

10

11

Bit

P1

P2

0

P4

1

0

1

P8

1

0

1

Calculating parity bits for odd parity:

  • P1 covers 3, 5, 7, 9, 11 = 0, 1, 1, 1, 1 → four 1s (even) → to make the group odd, P1 = 1
  • P2 covers 3, 6, 7, 10, 11 = 0, 0, 1, 0, 1 → two 1s (even) → P2 = 1
  • P4 covers 5, 6, 7 = 1, 0, 1 → two 1s (even) → P4 = 1
  • P8 covers 9, 10, 11 = 1, 0, 1 → two 1s (even) → P8 = 1

The transmitted sequence under odd parity is: 1 1 0 1 1 0 1 1 1 0 1.

Error detection and correction at the receiver follow the same steps as the even-parity example: recompute each group, this time expecting an odd count, and any group whose count comes out even flags a mismatch. The syndrome bits (C8 C4 C2 C1) are combined the same way to locate a flipped bit.

AWS Cloud Architect Masters ProgramEXPLORE PROGRAM
Advance Your Skills in AWS Cloud Architecture

Advantages of Hamming Code

  • Detects and corrects single-bit errors automatically, without needing the sender to retransmit data.
  • Detects double-bit errors, even though it cannot correct them.
  • Simple to implement in both hardware and software using basic XOR-style parity logic.
  • Fast to compute compared to more complex error-correcting codes, making it practical for real-time systems like memory controllers.
  • Widely supported and well understood, since it has been a standard technique since the 1950s.

Disadvantages and Limitations of Hamming Code

  • Cannot correct errors when 2 or more bits are flipped in the same block; it can only detect that something went wrong.
  • Adds overhead: for small data blocks, the number of redundant bits can be a significant fraction of the total transmitted bits.
  • It is not efficient for correcting burst errors (multiple consecutive bits flipped together), which are common in real-world noisy channels; other codes, such as Reed-Solomon codes, are better suited for that.
  • As data size grows, you need more redundant bits, increasing the overhead-to-data ratio. At the same time, it improves for larger blocks, but still adds design complexity for very large messages compared to simpler checksum methods.

Applications of Hamming Code

  • ECC (Error-Correcting Code) RAM: Used in servers and mission-critical systems to detect and correct single-bit memory errors caused by electrical noise or cosmic radiation, without crashing the system.
  • Satellite and space communication: Used where retransmission is slow or impossible due to distance, so correcting errors on arrival matters more than in typical networks.
  • Embedded systems and microcontrollers: Used to protect data integrity in low-power devices with limited processing capacity for more complex error-correction schemes.
  • Data storage systems: Used in some storage and modem technologies to catch and fix single-bit corruption during read/write or transmission.
This step-by-step Cloud Engineer roadmap is designed for professionals seeking to understand the full scope of the profession. Explore the skills, tools, salary potential, and career roadmap needed to build a successful career as a Cloud Engineer.

Conclusion

Hamming code is a fundamental error-correction technique that helps maintain data integrity by adding redundant parity bits to transmitted or stored data. It can detect up to two-bit errors and automatically correct a single-bit error, making it useful in applications such as ECC memory, embedded systems, and communication technologies. Understanding Hamming code also provides a foundation for learning how computer systems and networks protect data as it moves between devices.

If you're looking to build on these networking and data communication concepts, cloud computing is a natural next step. Modern cloud environments rely on reliable networks, secure data transfer, scalable infrastructure, and resilient systems to keep applications and services running. Explore Simplilearn's Cloud Computing Courses to develop practical skills in cloud architecture, infrastructure, and cloud technologies and prepare for roles in cloud computing.

Key Takeaways

  • Hamming code adds redundant parity bits to data so a receiver can detect and correct errors without retransmission.
  • The number of redundant bits needed is found using 2^r ≥ d + r + 1.
  • Parity bits sit at power-of-2 positions (1, 2, 4, 8, ...) and each covers a specific group of positions based on binary representation.
  • Hamming code detects up to 2-bit errors but corrects only 1-bit errors; this limit comes from its minimum Hamming distance of 3.
  • Hamming distance measures how many bit positions two codewords differ in; Hamming code uses that concept to build error-resilient codewords.
  • Common applications include ECC RAM, satellite communication, and embedded systems.

FAQ

1. What is Hamming code 7,4?

Hamming(7,4) is a specific example of a Hamming code with a length of 7 bits (3 parity bits + 4 data bits). This code is the easiest to understand because it encodes only 4 data bits with 3 parity bits. Hamming (7,4) codes are also used to introduce more complex Hamming codes, such as (11,7) (7 data bits, 4 parity bits), which we discussed above.

2. What is Hamming code used for in computer networks?

It is used in the data link layer to protect the data from short-distance transmission against single-bit corruption. Hamming code is more common in ECC memory or for short-distance communication than across the whole Internet because more complex, reliable error-checking mechanisms, such as CRC or retransmission, are typically used.

3. How is Hamming code different from CRC (Cyclic Redundancy Check)?

CRC is a more modern and efficient error-control method compared to Hamming codes. CRCs are exceptionally good at detecting errors, especially burst errors, but they cannot correct them. Therefore, if a CRC fails, the message must be resent, which is inefficient. Hamming codes introduce less overhead and can correct single-bit errors immediately.

4. What is the difference between a simple parity bit and Hamming code?

With a single parity bit, you can check whether an error occurred in a block; however, you cannot identify which bit is incorrect. Using Hamming codes, one can pinpoint the exact source of the error and correct it.

About the Author

Anmol KapoorAnmol Kapoor

Anmol is a Research Analyst who aims to become a Data Scientist one day. He enjoys Data Management systems and analysis. You will find him reading a book when he is not working.

View More
  • Acknowledgement
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, OPM3 and the PMI ATP seal are the registered marks of the Project Management Institute, Inc.
  • *All trademarks are the property of their respective owners and their inclusion does not imply endorsement or affiliation.
  • Career Impact Results vary based on experience and numerous factors.