Hamming Code in Computer Network: Working, Types & Uses

Blog Featured image for blog: hamming code in computer network

Categories

Introduction

Data travels fast across cables, through the air, over fiber, and sometimes, it breaks along the way. A bit flip means 1 becomes a 0. The message arrives wrong, and the receiver doesn’t know.

That’s the problem hamming code in computer network solves. It catches the mistake, finds where it happened, and then fixes it. There is no need for retransmission or asking the sender to repeat. The receiver handles it on its own.

In this blog, we will discuss what is hamming code, how it works, why it matters, and a clear hamming code example so you actually understand it instead of just memorizing steps.

Before getting into more details, let us first understand what Hamming code really is.

What Is Hamming Code?

Hamming code in computer network is a method for detecting and correcting errors in data. When you send bits from one place to another, some bits might flip. It happens because of many reasons, including noise on the wire, interference in the air, or a glitch in memory.

Hamming code adds extra bits to your data. These extra bits are called parity bits or redundant bits. They don’t carry your actual information. They carry checks. They watch over specific groups of data bits. If something goes wrong, the parity bits tell you exactly which bit flipped.

The beauty of it is efficiency. You don’t double your data. You don’t triple it. You add just a few extra bits. And those few bits give you the power to find and fix a single-bit error.

Why Hamming Code Matters in Computer Networks?

Each time data is transferred across networks, there is a risk, including electromagnetic interference or signal degradation and crosstalk between cables, and also corruption of the packet. These aren’t uncommon events.

Most of the time, protocols like TCP catch errors and request retransmission. That works fine when you have a two-way channel. But what about situations where you can’t ask for a resend?

For example, satellite links, deep space communication, real-time audio streams, and broadcast transmissions. In these cases, the data goes one way. If it arrives broken, you need to fix it on the spot. That’s where hamming code in computer network shines. It gives the receiver the ability to correct errors without talking back to the sender.

Before we get into the functioning of the Hamming code, it is better to understand two concepts, i.e., redundant bits and parity.

The Building Blocks: Redundant Bits and Parity

Redundant bits

Redundant bits are extra bits added to your data. They don’t carry your message. They carry error-checking information. The number of redundant bits you’ll need is totally dependent on the number of data bits you’re transmitting. The formula is:

2^r ≥ m + r + 1

Where m is the number of bits in data and r is the number of redundant bits.

Let us take an example for better understanding where you have four data bits. Input r = 3.

Put the values in the above formulae, i.e., m=4, and r=3

2^3  ≥  4+3+1

As you can see the values are same for both LHS and RHS.Therefore, you require three redundant bits to make four bits of data. The length of the codeword is total of 7 bits. This is the famous Hamming (7, 4) code.

Let’s say you have seven data bits.

Again, put the values in the formulae, i.e., m=7 and for r Taking m=7 and r = 3

2^3≥ 7+3+1 (As you can see r can’t be 3 as the RHS will be greater than LHS. Then it won’t work. )

You’ll need r = 4.

Because 2^4 ≥7 + 4+1 equals 12. Sixteen is higher than twelve. It is a valid code. The total codeword is 11 bits.

Parity

Parity is simpler. Count the number of 1s in the group of bits. If the count is even, even parity give you 0. If the count is not even i.e., odd then even parity gives you 1. The parity bit makes the total count of 1s to even. That’s it.

How Hamming Code Works?

Let us understand the working of Hamming Code with the help of an example.

Hamming Code Example

Let’s work through a real hamming code example. Say your data is 1011. Four data bits.

From the formula, we need 3 parity bits. So the total codeword is 7 bits long.

Now before we set anything up, you need to understand where parity bits and data bits go. Parity bits only sit at power-of-two positions. No exceptions. That means:

  • Position 1 → 2⁰ → Parity bit (P1)
  • Position 2 → 2¹ → Parity bit (P2)
  • Position 4 → 2² → Parity bit (P4)

Every other position holds a data bit. Position 3 is not a power of two. So it gets your first data bit, not a parity bit. There is no P3 in hamming code. Never will be.

PositionPower of 2?What goes here
1Yes (2⁰)P1
2Yes (2¹)P2
3NoD1
4Yes (2²)P4
5NoD2
6NoD3
7NoD4

Now plug in your data bits. Your message is 1011. So D1 = 1, D2 = 0, D3 = 1, D4 = 1.

Position1234567
TypeP1P2D1P4D2D3D4
Value??1?011

Parity bits are still unknown. Time to calculate them. Each parity bit checks specific positions based on binary. Here’s the rule. Write every position number in binary. If a position has a 1 in the first bit (rightmost), P1 covers it. If it has a 1 in the second bit, P2 covers it. If it has a 1 in the third bit, P4 covers it.

PositionBinary1st bit (P1)2nd bit (P2)3rd bit (P4)
10011 ✅0 ❌0 ❌
20100 ❌1 ✅0 ❌
30111 ✅1 ✅0 ❌
41000 ❌0 ❌1 ✅
51011 ✅0 ❌1 ✅
61100 ❌1 ✅1 ✅
71111 ✅1 ✅1 ✅

Now calculate each parity bit.

P1 checks positions 1, 3, 5, 7. We already know the data values at positions 3, 5, and 7. That’s 1, 0, and 1. Two 1s. Even count. So P1 = 0.

P2 checks positions 2, 3, 6, 7. Data at positions 3, 6, and 7 gives us 1, 1, and 1. Three 1s. Odd count. So P2 = 1.

P4 checks positions 4, 5, 6, 7. Data at positions 5, 6, and 7 is 0, 1, and 1. Two 1s. Even count. So P4 = 0.

Fill everything in.

Position1234567
Value0110011

The final codeword: 0110011.

That’s your message wrapped in protection. Seven bits total. Four carrying your actual data and three standing guard to catch and fix any single-bit error.

How Error Detection and Correction Works?

Now the good part. Say the receiver gets 0100011. Bit 3 flipped from 1 to 0 during transmission.

The receiver doesn’t know which bit is wrong. But hamming code tells them. Here’s how.

Recalculate each parity check:

P1 checks positions 1, 3, 5, 7: values are 0, 0, 0, 1. Count of 1s = 1. Odd. Flag it. Result = 1.

P2 checks positions 2, 3, 6, 7: values are 1, 0, 1, 1. Count of 1s = 3. Odd. Flag it. Result = 1.

P4 checks positions 4, 5, 6, 7: values are 0, 0, 1, 1. Count of 1s = 2. Even. No flag. Result = 0.

Read the results from P4 to P1: 011. In decimal, that’s 3. Position 3 has the error. Flip it. The 0 becomes 1 again. Data restored.

Types of Hamming Code

Different types of Hamming code in computer network are:

  • Hamming (7, 4) can be described as the standard. Four data bits and three parity bits,i.e., seven total. It is responsible for correcting single-bit errors. This is the most common method.
  • Hamming (11, 7) is able to handle seven data bits using a parity bit with four bits. It follows the same principle, just a bit bigger.
  • Extended Hamming Code adds one additional parity bit over the standard code. This second part includes all the codewords. It is the result of a code that is able to correct single-bit errors as well as detect double-bit errors. This is the version that’s also known as SECDED. With its help, one can handle single error correction and double error detection. ECC memory inside your computer utilizes this.

Where Hamming Code Is Used in Real Life

You might think this is just textbook concept, It’s not. Below, we have discussed Hamming code use cases in real life.

  • ECC Memory: Your server’s RAM utilizes extended Hamming code in order to detect and correct bit flips. The cosmic rays, voltage fluctuations, and heat can flip bits in memory. ECC is able to catch these before they can cause data corruption or crashes.
  • Satellite Communication: Satellite-generated data can’t be retransmitted easily. Its signal traverses billions of miles. Hamming code allows ground stations to repair errors after arriving.
  • Data Storage: HDDs as well as SSDs utilize error-correcting codes. Hamming code is among the simplest codes that are used in various storage systems.
  • Embedded Systems: Devices that have limited bandwidth and no channel for retransmission depend on Forward error correction. Hamming code works because it’s light and fast.
  • Telecommunications: In noisy channels where retransmissions are costly and slow, the Hamming code decreases the error rate without additional costs.

Hamming Code vs. Other Error Correction Methods

Hamming code isn’t the only game in town. Here’s how it stacks up.

  1. Parity Check: A single parity bit detects errors but can’t correct them. Hamming code does both.
  2. Repetition Code: Send every bit three times. Compare at the receiver. It works, but wastes bandwidth. Hamming code is far more efficient.
  3. CRC (Cyclic Redundancy Check): Great at detecting errors. Used in Ethernet frames and file transfers. But CRC doesn’t correct errors. It just flags them for retransmission.
  4. Reed-Solomon Code: Handles burst errors. Used in CDs, DVDs, QR codes, and deep space communication. More powerful than the Hamming code but more complex.

Strengths of Hamming Code

Hamming code earns its place for good reasons. Some of these are:

  • It’s efficient. You add very few extra bits relative to the data. A Hamming (7, 4) code has a rate of 4/7. That’s about 57% efficiency. Not bad for error correction.
  • It’s fast. The math is simple, i.e., XOR operations and parity checks. No heavy computation. Hardware can do it in real time.
  • It’s proven. It has been used for decades in memory, communication, and storage.
  • It’s elegant. The way parity bits map to bit positions using powers of two is clean. The binary trick for finding the error position is brilliant in its simplicity.

Limitations of Hamming Code

Hamming code isn’t perfect for every situation. Some of the limitations of Hamming code are:

  • It corrects only single-bit errors. If two bits flip in the same codeword, the standard Hamming code will “correct” the wrong bit. It makes things worse. An extended Hamming code detects double-bit errors but can’t fix them.
  • It doesn’t handle burst errors well. In noisy channels where multiple consecutive bits get corrupted, the Hamming code falls short. You need something stronger, like Reed-Solomon codes, for that.
  • It adds overhead. For small data blocks, the ratio of parity bits to data bits is high. For a 4-bit message, you add 3 parity bits. That’s 75% overhead. As data blocks grow, the overhead shrinks. But for tiny messages, it’s costly.

Frequently Asked Questions

Q1. What is the Hamming code for 101011?

If the parity bit is even, the Hamming code for 101011 would be 0111010011. It is a combination of four parity bits in order to find and correct a misplaced bit of transmission.

Q2. What is the 7-bit Hamming code?

The seven-bit Hamming code is called Hamming 7,4. It carries four data bits and three parity bits, and it corrects one error during transit.

Q3. Why do we need Hamming code?

We need the Hamming code because data can change while moving. It finds and fixes a single bit error, which makes communication more reliable for systems.

Q4. What is the 7-bit Hamming code for the 4 bit data 1011?

Assuming that parity is even parity, the seven-bit Hamming code for 1011 is 0110011. The three parity bits have been inserted within the data bits to provide protection during transport.

Conclusion

Hamming code solves a real problem. Data breaks during transmission. Bits flip. Messages arrive incorrectly. Instead of throwing everything away and starting over, hamming code finds the broken bit and fixes it.

If you’re studying computer networks, the Hamming code is where error correction starts. Learn it well. The principles carry forward into every protocol and system you’ll ever work with. The data will always face noise. The question is whether your system can handle it. Hamming code says yes.

Any Questions?
Get in touch

Blog

Get Free Career Guidance

Popular Courses

Leave a Reply

Your email address will not be published. Required fields are marked *