# Hamming code in C

Updated on September 19, 2011

## Hamming Code Algorithm

In telecommunication, a Hamming code is a linearerror-correcting code named after its inventor, Richard Hamming. Hamming codes can detect up to two simultaneous bit errors, and correct single-bit errors; thus, reliable communication is possible when the Hamming distance between the transmitted and received bit patterns is less than or equal to one. By contrast, the simple parity code cannot correct errors, and can only detect an odd number of errors.

In mathematical terms, Hamming codes are a class of binary linear codes. For each integer there is a code with m parity bits and 2mm − 1 data bits. The parity-check matrix of a Hamming code is constructed by listing all columns of length m that are pairwise independent. Hamming codes are an example of perfect codes, codes that exactly match the theoretical upper bound on the number of distinct code words for a given number of bits and ability to correct errors.

Because of the simplicity of Hamming codes, they are widely used in computer memory (RAM). In particular, a single-error-correcting and double-error-detecting variant commonly referred to as SECDED.

## Hamming code in C

/* Program to find hamming code*/
#include<stdio.h>
#include<stdlib.h>

char data[5];
int encoded[8],edata[7],syndrome[3];
int hmatrix[3][7] =	{
1,0,0,0,1,1,1,
0,1,0,1,0,1,1,
0,0,1,1,1,0,1
};
char gmatrix[4][8]={"0111000","1010100","1100010","1110001"};

int main(){
int i,j;
system("clear");
printf("\nHamming code----- Encoding\n");
printf("Enter 4 bit data : ");
scanf("%s",data);
printf("\nGenerator matrix\n");
for(i=0;i<4;i++)
printf("%s\n",gmatrix[i]);
printf("\nEncoded data ");
for(i=0;i<7;i++)
{
for(j=0;j<4;j++)
encoded[i]+=((data[j]-'0')*(gmatrix[j][i]-'0'));
encoded[i]=encoded[i]%2;
printf("%d ",encoded[i]);
}
printf("\nHamming code----- Decoding\n");
printf("Enter encoded bits as recieved : ");
for(i=0;i<7;i++)
scanf("%d",&edata[i]);
for(i=0;i<3;i++)
{
for(j=0;j<7;j++)
syndrome[i]+=(edata[j]*hmatrix[i][j]);
syndrome[i]=syndrome[i]%2;
}
for(j=0;j<7;j++)
if((syndrome[0]==hmatrix[0][j]) && (syndrome[1]==hmatrix[1][j])&& (syndrome[2]==hmatrix[2][j]))
break;
if(j==7)
printf("\nError free\n");
else
{
printf("\nError recieved at bit number %d of data\n",j+1);
edata[j]=!edata[j];
printf("\nCorrect data should be : ");
for(i=0;i<7;i++)
printf("%d",edata[i]);
}
return 0;
}

## Makefile

a.out:hammingCode.c
gcc -ggdb hammingCode.c
PHONY:clean
clean:
rm a.out *~

## Example output

Hamming code----- Encoding
Enter 4 bit data : 1011

Generator matrix
0111000
1010100
1100010
1110001

Encoded data 0 1 0 1 0 1 1
Hamming code----- Decoding
Enter encoded bits as received : 0 1 0 1 1 1 1

Error received at bit number 5 of data

Correct data should be : 0101011

14

7

0

12

## Popular

11

22

• ### Types of Function in C Programming Language

164

0 of 8192 characters used

• Arsalan Khan

20 months ago

This Program is not working in Borland C++....Can you please post the same code for borland c++....it will be my pleasure...

• kgiyu

5 years ago

ukfkf

• jack

7 years ago

nice please display hamming code program for 7 bit

• jim

7 years ago

^^^^^^^^^^^

working