Hamming Code | C++ Program To Implement Hamming Code

Hamming Code | Hamming Code error correction

Hamming code is a set of error-correction codes that can be utilized to detect and correct the errors that can happen when the data is moved or put away from the sender to the receiver. 

What Are Parity bits

A parity bit is a bit affixed to a data of binary bits to guarantee that the absolute number of 1's in the data is even or odd. Parity bits are utilized for error detection. 


There are two types of parity bits:


even parity bit:
On account of even parity, for a given set of bits, the quantity of 1's are checked. In the event that that check is odd, the parity bit esteem is set to 1.

Odd Parity bit : 

On account of odd parity, for a given set of bits, the quantity of 1's are tallied. On the off chance that that check is even, the parity bit esteem is set to 1.


Hamming Code Program in C++


#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;
}

OUTPUT:
At Sender's side :
Enter the message in binary : 1001101
Odd(O)/Even(E) Parity ? E
Number of Check Bits : 4
Number of Bits in Codeword : 11
Parity Bits - P1 : 0    P2 : 1 P4 : 1 P8 : 0    
Codeword :
0 1 1 1 0 0 1 0 1 0 1

At Receiver's Side :
Enter the received codeword : 01110010101
Parity Bits - P1: 0    P2: 0 P4: 0 P8: 0    
Received Codeword :
0 1 1 1 0 0 1 0 1 0 1
No Error in Received code.
Received Message is : 1 0 0 1 1 0 1

Enter another code ? (Y/N) : N




You must be also searching for these programming languages :




tags : Hamming code, hamming code error correction,
 computer networks, computer networks programs, computer networks programming language.

2 Comments

Previous Post Next Post