# Implementation of Hamming code

By | January 10, 2016

This post gives an idea to implement Hamming Code project. 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 encoder to 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 Haming 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) = # of bit positions in which a differs from b, i.e., the number of 1’s in a 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  Requirements:-

• 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 is also 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 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 brust error (A burst error means that 2 or more bits in the data unit have changed),but by  applying  different technique.

The structure of the encoder and decoder for a Hamming code

The structure of the encoder and decoder for a Hamming code

#### The c code to implement hamming code is given below

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

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

.

.

Author: Ravi Bandakkanavar

A Techie, Blogger, Web Designer, Programmer by passion who aspires to learn new Technologies every day. It has been 6 years since I have been publishing articles and enjoying every bit of it. I want to share knowledge and build a great community with people like you.