PROJECTS

# Implementation of Hamming code

• This post gives an idea on how to implement Hamming Code. Hamming code pattern is mainly used for detection of errors in bits.

## Objectives:

• The main objective is, detection of single bit error, burst error and to correct by using the suitable methodology (by hamming distance concept).
• The purpose of this project is to overcome unreliable transmission of data during the transmission from the encoder to the decoder.
• It involves block coding, finding the hamming distance between to existing code words, redundancy, error correction, parity check code.

## Description:

The theory of error detecting and correcting codes is that branch of engineering and mathematics which deals with the reliable transmission and storage of data. Information media are not 100% reliable in practice, in the sense that noise (any form of interference) frequently causes data to be distorted. To deal with this undesirable but inevitable situation, self-correcting codes are constructed by combining data bits with a number of redundant check-bits, which together are referred to as code words.  By adding a single bit (as in Parity Checking) along with a given number of data-bits, half of the possible code words become valid and half invalid.  This is referred to as a code that has Hamming Distance 2 (two single bit flips are required to get from one calid code to another). This particular approach, using Hamming Distance, is the minimum requirement for self-error correction and is commonly referred to as a Hamming Code.

## Preliminaries:

a. Hamming distance H(a,b) = #number of bit positions in which a differs from b, i.e., the number of 1’s in an XOR. Hamming distance of a code is the minimum over all pairs of distinct code words of the Hamming distance between them, i.e.,

H(code) = min {H(a,b) | a<>b and a, b in code}

b. Error correction – enough redundancy is transmitted in the code that errors can be corrected by the receiver without retransmission.

c. Error detection is the detection of errors caused by noise or other impairments during transmission from the transmitter to the receiver.

d. d-bit error detection requires a code with Hamming distance (d+1).
d-bit error correction requires a code with Hamming distance (2d+1).

## Tools  Required:

• Encoder.
• Decoder.
• cyclic redundancy check (CRC) or polynomial code checksum.
• linear block codes.
• Two single bit Flip-Flop.

## Future Scope:

In the field of artificial intelligence, where there is a need to search through large state spaces, there is the notion of using evaluation functions to heuristically search a large space in a hill climbing or the best-first search fashion. Since Hamming distance is an easy-to-define metric, it is used to search the state space for design flaws.

Hamming code is an easy and efficient technique, which can only detect and correct a single bit error. However. it can also be used to detect a burst error (A burst error means that 2 or more bits in the data unit have changed), but by applying the different technique.

The structure of the encoder and decoder for a Hamming code

Structure of the encoder and decoder for a Hamming code

#### Following is the code in C language to implement hamming code:

Program to implement 7-bit hamming code:

```#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);

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 && dataatrec==data && dataatrec==data && dataatrec==data ) {
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]);
}
}
}```

```//HAMMING CODE for error detection and correction
#include<stdio.h>
int main()
{
int a,b,r, s,i,q;
printf("\nenter 4 bit data word:\n");
for(i=3;i>=0;i--)
{
scanf("%d",&a[i]);
}
r=(a+a+a)%2;
r=(a+a+a)%2;
r=(a+a+a)%2;

printf("\n\nthe 7bit hamming code word: \n");
for(i=3;i>=0;i--)
{
printf("%d\t",a[i]);
}
for(i=2;i>=0;i--)
{
printf("%d\t",r[i]);
}
printf("\n");
printf("\nenter the 4bit recieved word: ");

for(i=3;i>=0;i--)
scanf("%d",&b[i]);

//calculating syndrome bits
s= (b+b+b+r)%2;
s= (b+b+b+r)%2;
s= (b+b+b+r)%2;
printf("\nsyndrome is: \n");

for(i=2; i>=0;i--)
{
printf("%d",s[i]);
}
if((s==0) && (s==0) && (s==0))
printf("\n RECIEVED WORD IS ERROR FREE\n");
if((s==1) && (s==0) && (s==1))
printf("\nrecieved word is error with error in 1bit(b0) position from right\n");
if((s==1) && (s==1) && (s==1))
printf("\nrecieved word is error with error in 2bit(b1) position from right\n");
if((s==0) && (s==1) && (s==1))
printf("\nrecieved word is error with error in 3bit(b2) position from right\n");
if((s==1) && (s==1) && (s==0))
printf("\nrecieved word is error with error in 4bit(b3) position from right\n");
return(1);
}
//End of Hamming code program```
Ravi Bandakkanavar

A Techie, Blogger, Web Designer, Programmer by passion who aspires to learn new Technologies every day. A founder of Krazytech. It's been 10+ years since I am publishing articles and enjoying every bit of it. I want to share the knowledge and build a great community with people like you.

• sahilvaghasiya9687gmailcom says:

using this formula 2^r>=m+r+1 then create program

• KD7 says:

This is correct code for 7bit hamming code please refer this:--------
#include

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

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 && dataatrec==data && dataatrec==data && dataatrec==data ) {
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]);
}
}
}

• Ravi Bandakkanavar says:

Thanks for the code Kedar. Is it tested one?

• Dhrisya krishna says:

How this error can be implemented as application level in project

Share
Ravi Bandakkanavar

## Everything You Need To Know About Create React App

The configuration of resources can be a time-consuming and difficult operation while creating a React…

3 weeks ago

## Causes of CyberCrime and Preventive Measures

Rapid technological growth and developments have provided vast areas of…

1 month ago

## How Data Lineage will Improve Business Practices

How often have you thought about changing the way that you store and use data?…

1 month ago

## 10 Dominating Programming Language for Mobile App In 2022

Programming Languages are a set of rules that aid in transforming a concept into a…

1 month ago

## What is Serverless Edge Computing? Features | Benefits

Serverless edge computing is a new technology with a lot of promise, but it can…

1 month ago

## Are Your Accounts Protected Against Cyberattacks?

Do any of your passwords include personal names, date of birth, or pet names? If…

2 months ago