# Hamming Code in C and C++

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

Hamming code is a prevalent blunder location and mistake revision technique in information

correspondence. Hamming code can just identify 2 piece mistake and right a solitary piece blunder

which means it can’t right blast mistakes if may happen while transmission of information.

Hamming code utilizes repetitive bits (additional bits) which are determined by the underneath equation:-

2r ≥ m+r+1

Where r is the number of excess bits required and m is the number of information bits.

R is determined by putting r = 1, 2, 3 … until the above condition turns out to be valid.

R1 bit is affixed at position 20

R2 bit is attached at position 21

R3 bit is affixed at position 22, etc.

These repetitive bits are then added to the first information for the estimation of a mistake at beneficiary’s end.

At collector’s end with the assistance of even equality (for the most part), the wrong piece position is distinguished and since information is in paired we take a supplement of the mistaken piece position to address got information.

Individual list equality is determined for r1, r2, r3, r4, etc.

Points of interest of Hamming Code

Simple to encode and unravel information at both sender and recipient end.

Simple to execute.

Drawbacks of Hamming Code

Can’t right blast mistakes.

Repetitive bits are additionally sent with the information in this way it requires more transfer speed to send the information.

Program for it in C

``````#include<stdio.h>

void main() {
int data;
int dataatrec,c,c1,c2,c3,i;

printf("Enter 4 bits of data one by one\n");
scanf("%d",&data);
scanf("%d",&data);
scanf("%d",&data);
scanf("%d",&data);

//Calculation of even parity
data=data^data^data;
data=data^data^data;
data=data^data^data;

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^dataatrec^dataatrec^dataatrec;
c2=dataatrec^dataatrec^dataatrec^dataatrec;
c3=dataatrec^dataatrec^dataatrec^dataatrec;
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;
int dataatrec,c,c1,c2,c3,i;

cout<<"Enter 4 bits of data one by one\n";
cin>>data;
cin>>data;
cin>>data;
cin>>data;

//Calculation of even parity
data=data^data^data;
data=data^data^data;
data=data^data^data;

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^dataatrec^dataatrec^dataatrec;
c2=dataatrec^dataatrec^dataatrec^dataatrec;
c3=dataatrec^dataatrec^dataatrec^dataatrec;
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``````

Remark beneath on the off chance that you have any questions identified with above hamming code program in C and C++.