0% found this document useful (0 votes)
9 views11 pages

Part - 1

The document implements Hamming code in C++ to detect and correct single bit errors that may occur during data transmission. It defines a Hamming class with functions to generate the codeword by inserting parity bits, detect errors by checking parity bits, and correct any errors found by flipping the detected bit. The main function allows generating and correcting codewords repeatedly based on user input. Key aspects included are calculating the number of check bits needed, inserting parity bits according to their position, and detecting/correcting errors by comparing the received parity bits.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views11 pages

Part - 1

The document implements Hamming code in C++ to detect and correct single bit errors that may occur during data transmission. It defines a Hamming class with functions to generate the codeword by inserting parity bits, detect errors by checking parity bits, and correct any errors found by flipping the detected bit. The main function allows generating and correcting codewords repeatedly based on user input. Key aspects included are calculating the number of check bits needed, inserting parity bits according to their position, and detecting/correcting errors by comparing the received parity bits.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Scanned by CamScanner

Scanned by CamScanner
Name : Upper Chenna keshava
Reg No:19BCI0170

Code for parity checking


#include <stdio.h>

int main()
{
int n;
printf("Enter the no. of bits of data\n");
scanf("%d",&n);
int ch, data[n];
printf("Select the parity type:-\n1.Even Parity\n2.Odd parity");
scanf("%d",&ch);
printf("Enter the data for which you want to calculate the parity one by one\n");
int i;
for(i=0;i<n;i++){
scanf("%d",&data[i]);
}
printf("Data sent by the sender = ");
for(i=0;i<n;i++){
printf("%d",data[i]);
}

int count=0,cnt;
for(i=0;i<n;i++){
if (data[i]==1)
count+=1;
cnt=count;
}
printf("\n\n%d",count);

int paritybit;
if (cnt%2==0)
paritybit=0;
else
paritybit=1;
printf("\nData sent by the sender along with even parity bit = ");
for(i=0;i<n;i++){
printf("%d",data[i]);
}
printf(" %d",paritybit);
}
Scanned by CamScanner
Scanned by CamScanner
Reg NO: 19CI0170
Name:Upper Chenna keshava

Implement Hamming Code using C++, C or Python.

Code:
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
class Hamming
{
string message;
int codeword[50],temp[50];
int n,check;
char parity;
public:
Hamming()
{
parity = 'E';
message = "";
n=check=0;
for(int i=0;i<50;i++)
{
temp[i]=codeword[i]=0;
}
}

void generate()
{
do
{
cout<<"Enter the message in binary : ";
cin>>message;
}while(message.find_first_not_of("01") != string::npos);
n=message.size();
cout<<"Odd(O)/Even(E) Parity ? ";
cin>>parity;
for(unsigned int i=0;i<message.size();i++)
{
if(message[i] == '1')
temp[i+1]=1;
else
temp[i+1]=0;
}
computeCode();
}
void computeCode()
{
check = findr();
cout<<"Number of Check Bits : "<<check<<endl;
cout<<"Number of Bits in Codeword : "<<n+check<<endl;
for(int i=(n+check),j=n;i>0;i--)
{
if((i & (i - 1)) != 0)
codeword[i] = temp[j--];
else
codeword[i] = setParity(i);
}
cout<<"Parity Bits - ";
for(int i=0;i<check;i++)
cout<<"P"<<pow(2,i)<<" : "<<codeword[(int)pow(2,i)]<<"\t";
cout<<endl;
cout<<"Codeword :"<<endl;
for(int i=1;i<=(n+check);i++)
cout<<codeword[i]<<" ";
cout<<endl;
}
int findr()
{
for(int i=1;;i++)
{
if(n+i+1 <= pow(2,i))
return i;
}}
int setParity(int x)
{
bool flag = true;
int bit;
if(x == 1)
{
bit = codeword[x+2];
for(int j=x+3;j<=(n+check);j++)
{
if(j%2)
{
bit ^= codeword[j];
}
}
}
else
{
bit = codeword[x+1];
for(int i=x;i<=(n+check);i++)
{
if(flag)
{
if(i==x || i==x+1)
bit = codeword[x+1];
else
bit ^= codeword[i];
}
if((i+1)%x == 0)
flag = !flag;
}
}
if(parity == 'O' || parity == 'o')
return !bit;
else
return bit;
}
void correct()
{
do
{
cout<<"Enter the received codeword : ";
cin>>message;
}while(message.find_first_not_of("01") != string::npos);
for(unsigned int i=0;i<message.size();i++)
{
if(message[i] == '1')
codeword[i+1]=1;
else
codeword[i+1]=0;
}
detect();
}
void detect()
{
int position = 0;
cout<<"Parity Bits - ";
for(int i=0;i<check;i++)
{
bool flag = true;
int x = pow(2,i);
int bit = codeword[x];
if(x == 1)
{
for(int j=x+1;j<=(n+check);j++)
{
if(j%2)
{
bit ^= codeword[j];
}
}
}
else
{
for(int k=x+1;k<=(n+check);k++)
{
if(flag)
{
bit ^= codeword[k];
}
if((k+1)%x == 0)
flag = !flag;
}
}
cout<<"P"<<x<<": "<<bit<<"\t";
if((parity=='E' || parity == 'e') && bit==1)
position += x;
if((parity=='O' || parity == 'o') && bit==0)
position += x;
}
cout<<endl<<"Received Codeword :"<<endl;
for(int i=1;i<=(n+check);i++)
cout<<codeword[i]<<" ";
cout<<endl;
if(position != 0)
{
cout<<"Error at bit : "<<position<<endl;
codeword[position] = !codeword[position];
cout<<"Corrected Codeword : "<<endl;
for(int i=1;i<=(n+check);i++)
cout<<codeword[i]<<" ";
cout<<endl;
}
else
cout<<"No Error in Received code."<<endl;
cout<<"Received Message is : ";
for(int i=1;i<=(n+check);i++)
if((i & (i - 1)) != 0)
cout<<codeword[i]<<" ";
cout<<endl;
}
};
int main()
{
char
choice; do
{
Hamming a;
cout<<"At Sender's side :
"<<endl; a.generate();
cout<<endl<<"At Receiver's Side : "<<endl;
a.correct();
cout<<endl<<"Enter another code ? (Y/N) : ";
cin>>choice;
cout<<endl;
}while(choice == 'y' || choice == 'Y');
return 0;
}

You might also like