# Hamming Code in C and C++

Here you will get program for hamming code in C and C++.

Hamming code is a popular error detection and error correction method in data communication. Hamming code can only detect 2 bit error and correct a single bit error which means it is unable to correct burst errors if may occur while transmission of data.

Also Read: Checksum Program in C and C++

Hamming code uses redundant bits (extra bits) which are calculated according to the below formula:-

2r ≥ m+r+1

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

R is calculated by putting r = 1, 2, 3 … until the above equation becomes true.

R1 bit is appended at position 20

R2 bit is appended at position 21

R3 bit is appended at position 22 and so on.

These redundant bits are then added to the original data for the calculation of error at receiver’s end.

At receiver’s end with the help of even parity (generally) the erroneous bit position is identified and since data is in binary we take complement of the erroneous bit position to correct received data.

Respective index parity is calculated for r1, r2, r3, r4 and so on.

Image Source

1. Easy to encode and decode data at both sender and receiver end.
2. Easy to implement.

1. Cannot correct burst errors.
2. Redundant bits are also sent with the data therefore it requires more bandwidth to send the data.

## Program for Hamming Code in C

```#include<stdio.h>

void main() {
int data[10];
int dataatrec[10],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]);

printf("\n\nEnter received data bits one by one\n");
for(i=0;i<7;i++)
scanf("%d",&dataatrec[i]);

c1=dataatrec[6]^dataatrec[4]^dataatrec[2]^dataatrec[0];
c2=dataatrec[5]^dataatrec[4]^dataatrec[1]^dataatrec[0];
c3=dataatrec[3]^dataatrec[2]^dataatrec[1]^dataatrec[0];
c=c3*4+c2*2+c1 ;

if(c==0) {
printf("\nNo error while transmission of data\n");
}
else {
printf("\nError on position %d",c);

printf("\nData sent : ");
for(i=0;i<7;i++)
printf("%d",data[i]);

for(i=0;i<7;i++)
printf("%d",dataatrec[i]);

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

//if errorneous bit is 0 we complement it else vice versa
if(dataatrec[7-c]==0)
dataatrec[7-c]=1;
else
dataatrec[7-c]=0;

for (i=0;i<7;i++) {
printf("%d",dataatrec[i]);
}
}
}```

## Program for Hamming Code in C++

```#include<iostream>

using namespace std;

int main() {
int data[10];
int dataatrec[10],c,c1,c2,c3,i;

cout<<"Enter 4 bits of data one by one\n";
cin>>data[0];
cin>>data[1];
cin>>data[2];
cin>>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];

cout<<"\nEncoded data is\n";
for(i=0;i<7;i++)
cout<<data[i];

cout<<"\n\nEnter received data bits one by one\n";
for(i=0;i<7;i++)
cin>>dataatrec[i];

c1=dataatrec[6]^dataatrec[4]^dataatrec[2]^dataatrec[0];
c2=dataatrec[5]^dataatrec[4]^dataatrec[1]^dataatrec[0];
c3=dataatrec[3]^dataatrec[2]^dataatrec[1]^dataatrec[0];
c=c3*4+c2*2+c1 ;

if(c==0) {
cout<<"\nNo error while transmission of data\n";
}
else {
cout<<"\nError on position "<<c;

cout<<"\nData sent : ";
for(i=0;i<7;i++)
cout<<data[i];

for(i=0;i<7;i++)
cout<<dataatrec[i];

cout<<"\nCorrect message is\n";

//if errorneous bit is 0 we complement it else vice versa
if(dataatrec[7-c]==0)
dataatrec[7-c]=1;
else
dataatrec[7-c]=0;
for (i=0;i<7;i++) {
cout<<dataatrec[i];
}
}

return 0;
}```

Output

Enter 4 bits of data one by one
1
0
1
0

Encoded data is
1010010

Enter received data bits one by one
1
0
1
0
0
1
0

No error while transmission of data

Comment below if you have any queries related to above hamming code program in C and C++.

### 14 thoughts on “Hamming Code in C and C++”

1. This is the correct code please refer this:—-

#include
#include
void main() {
int data[10];
int dataatrec[10],c,c1,c2,c3,i;
clrscr();
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]);

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]);

printf("\n\nEnter received data bits one by one\n");
for(i=0;i<7;i++)
scanf("%d",&dataatrec[i]);

c1=dataatrec[6]^dataatrec[4]^dataatrec[2]^dataatrec[0];
c2=dataatrec[5]^dataatrec[4]^dataatrec[1]^dataatrec[0];
c3=dataatrec[3]^dataatrec[2]^dataatrec[1]^dataatrec[0];
c=c3*4+c2*2+c1 ;

if(c==0 && dataatrec[6]==data[6] && dataatrec[5]==data[5] && dataatrec[3]==data[3] ) {
printf("\nNo error while transmission of data\n");
}
else {
printf("\nError on position %d",c);

printf("\nData sent : ");
for(i=0;i<7;i++)
printf("%d",data[i]);

for(i=0;i<7;i++)
printf("%d",dataatrec[i]);

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

if(dataatrec[7-c]==0)
dataatrec[7-c]=1;
else
dataatrec[7-c]=0;

for (i=0;i<7;i++) {
printf("%d",data[i]);
}
}
}

1. Any exceptions ? Why ur adding if(c==0 && dataatrec[6]==data[6] && dataatrec[5]==data[5] && dataatrec[3]==data[3] )

2. i am enter 1111 as sent data and it shows that the ecoded data is 1111111. Fine but when i give it 1001011 as received data it shows me that there is No error while transmission of data.

3. I Have problem use hamming code encoder send 4 bits 0001 to encoder output 0000111 go to channel 7 bits 1000111 use Binary Symmetric Channel end to decoder output 4 bit some input 0001. and my problem is how to write program to create BSC use c++ please share source code.

Thanks

4. Input: A binary message
Output: The codeword length if the message is transmitted using hamming code and the position of the parity bits (string of 0’s and 1’s)
How to write a program in c/c++?