Table of contents
1.
Introduction
2.
Implementation in C
2.1.
C
3.
Error Detection & Correction
3.1.
C
4.
Frequently Asked Questions
4.1.
Why is Hamming Code Important?
4.2.
Can Hamming Code Detect Multiple Errors?
4.3.
Is Hamming Code Used Today?
5.
Conclusion
Last Updated: Dec 6, 2024
Easy

Hamming Code in C

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hamming Code is a set of error-correction codes that can detect & correct errors in data bits. It's a lifesaver in digital communications & computer engineering, ensuring the reliability of data transmission. This article will guide you through Hamming Code's concept, its implementation in C, & the practical aspects of error detection & correction. By the end, you'll grasp the fundamental workings of this coding technique, capable of implementing it in C, & understanding its real-world applications.

Hamming Code in C

Let's dive into the core of Hamming Code. It's a method developed by Richard Hamming in the 1950s to detect & correct errors in transmitted data. In the digital world, data often gets corrupted during transmission due to various factors like electrical noise or interference. Hamming Code helps in identifying & fixing these errors, ensuring the integrity of the data.

Implementation in C

Now, we'll look into implementing Hamming Code in C. This section will provide you with a step-by-step guide & a comprehensive code example.

  • Setting Up the Environment: Before diving into coding, make sure your C programming environment is set up. You can use any C compiler like GCC. Install it by running sudo apt-get install gcc on Linux or using appropriate methods for other operating systems.
     
  • Understanding the Logic: The essence of Hamming Code lies in its ability to add redundancy bits to the data. These redundancy bits are placed at specific positions (typically powers of 2) in the data sequence. Their values are determined based on the data bits, following certain parity rules.

Writing the Code: Here’s a simple code snippet for generating a Hamming Code.

  • C

C

#include <stdio.h>

#include <math.h>

int main() {

   int data[10];

   int data_at_reciever[10];

   int c,c1,c2,c3,i;


   printf("Enter 4 bits of data one by one\n");

   scanf("%d", &data[0]);

   scanf("%d", &data[1]);

   scanf("%d", &data[2]);

   scanf("%d", &data[4]);

   //Calculation of even parity

   data[6] = data[0] ^ data[2] ^ data[4];

   data[5] = data[0] ^ data[1] ^ data[4];

   data[3] = data[0] ^ data[1] ^ data[2];

   printf("\nEncoded data is\n");

   for (i = 0; i < 7; i++) {

       printf("%d ",data[i]);

   }

   return 0;

}
You can also try this code with Online C Compiler
Run Code

Output

Output

This code asks for 4 bits of data & then calculates the parity bits. It then displays the encoded data with parity bits.

Error Detection & Correction

After encoding data with Hamming Code, the next step is to detect & correct any errors in the received data.

  • Receiving Data: Let's say we receive data at the other end. There might be an error in any bit, including the parity bits.
     
  • Decoding the Data: We need to check the parity bits against the received data. If there's a discrepancy, it indicates an error.
     
  • Correcting the Error: By identifying which parity bits are incorrect, we can pinpoint the exact location of the error in the data & correct it.

Here's a sample code snippet for error detection & correction:

  • C

C

#include <stdio.h>

#include <math.h>

int main() {

   int data[10];

   int data_at_reciever[10];

   int c,c1,c2,c3,i;


   printf("Enter 4 bits of data one by one\n");

   scanf("%d", &data[0]);

   scanf("%d", &data[1]);

   scanf("%d", &data[2]);

   scanf("%d", &data[4]);

   //Calculation of even parity

   data[6] = data[0] ^ data[2] ^ data[4];

   data[5] = data[0] ^ data[1] ^ data[4];

   data[3] = data[0] ^ data[1] ^ data[2];


  printf("\nEnter received data bits one by one\n");

for (i = 0; i < 7; i++) {

   scanf("%d", &data_at_reciever[i]);

}



c1 = data_at_reciever[6] ^ data_at_reciever[4] ^ data_at_reciever[2] ^ data_at_reciever[0];

c2 = data_at_reciever[5] ^ data_at_reciever[4] ^ data_at_reciever[1] ^ data_at_reciever[0];

c3 = data_at_reciever[3] ^ data_at_reciever[2] ^ data_at_reciever[1] ^ data_at_reciever[0];

c = c3 * 4 + c2 * 2 + c1 ;



if(c == 0) {

   printf("No error while transmission of data\n");

}

else {

   printf("Error on position %d", c);   

   printf("\nData sent : ");

   for (i = 0; i < 7; i++) {

       printf("%d ", data[i]);

   }


   printf("\nData received : ");

   for (i = 0; i < 7; i++) {

       printf("%d ", data_at_reciever[i]);

   }


   printf("\nCorrect message is\n");


   //if errorneous bit is 0 we complement it else vice versa

   if(data_at_reciever[7-c] == 0) {

       data_at_reciever[7-c] = 1;

   }

   else {

       data_at_reciever[7-c] = 0;

   }

   for (i = 0; i < 7; i++) {

       printf("%d ",data_at_reciever[i]);

   }

}



return 0;

}
You can also try this code with Online C Compiler
Run Code

 Output

Output

This code segment takes the received data, calculates the parity again, & checks for any discrepancy. If an error is found, it identifies the bit's position & corrects it.

Frequently Asked Questions

Why is Hamming Code Important?

Hamming Code is crucial for ensuring data integrity in digital communications, preventing errors that can occur during data transfer.

Can Hamming Code Detect Multiple Errors?

It can detect two errors & correct one. However, correcting multiple errors requires more advanced techniques.

Is Hamming Code Used Today?

Absolutely! It's used in various applications like computer memory (RAM), data transmission protocols, & more.

Conclusion

Hamming Code in C demonstrates the marriage of theoretical knowledge & practical application in computer science. This article walked you through its basics, implementation, & error handling in C, offering a hands-on experience. Understanding & implementing such concepts is pivotal in your journey as a coding student, paving the way for more advanced studies & applications in the field of computer science.

 

Live masterclass