0% found this document useful (0 votes)
21 views6 pages

DCP10

The document discusses an implementation of Hamming code in C to detect and correct single bit errors. It includes functions to generate a codeword by adding redundant bits, transmit the codeword, receive the potentially corrupted codeword, and detect and correct a single bit error using the redundant bits.

Uploaded by

mitaleeextra
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)
21 views6 pages

DCP10

The document discusses an implementation of Hamming code in C to detect and correct single bit errors. It includes functions to generate a codeword by adding redundant bits, transmit the codeword, receive the potentially corrupted codeword, and detect and correct a single bit error using the redundant bits.

Uploaded by

mitaleeextra
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/ 6

CODE: HAMMING CODE

#include<stdio.h>
#include<math.h>
#include<stdlib.h>

// msg: 1 0 1 0
// received: 1 0 1 0 1 1 0

int main()
{
//BIT INDEXING-right to left
int m,r=0,msg[50],data[60],i,j,k;
printf("Enter message size: ");
scanf("%d",&m);

//calculate no of r bits
while(1)
{ //keep incrementing UNTIL 2^r >= m+r+1 satisfied
if((m+r+1)<=(int)pow(2,r))
break;
r++;
}

//input message
printf("Enter message bits (space separted): ");
for(i=m;i>=1;i--)//backwards because reverse indexing
{
scanf("%d",&msg[i]);
}
//filling the spaces to generate codeword
k=0; //for 2^k / position of r bits
j=1; //for position of msg bits
for(i=1;i<=(m+r);i++)
{
if(i==(int)pow(2,k)) //finding redundant bit positions
{ //at positions with powers of 2, put number 8 as a representation of r/empty
data[i]=8;
k++;
}
else
{ //at other positions, put message bits as they were
data[i]=msg[j];
j++;
}
}

//EVEN PARITY TO FILL R BITS


for(i=1;i<=(m+r);i++) //run for loop on all the bits of code word
{
if(data[i]==8)//if r bit encountered
{
data[i]=0; //set value=0
int count=0; //to keep count of number of 1's
//to find number of 1's
for(j=i;j<=(m+r);j++)
{
for(k=0;k<i;k++)
{
if(data[j]==1)
{
count++;
}
j++;
}
j=j+i-1;
}
//if even no. of 1's, r bit = 0 to keep it even
if(count%2==0)
{
data[i]=0;
}
//if odd no. of 1's, r bit = 1 to make it even
else
{
data[i]=1;
}
}
}

printf("Generated/Transmitted codeword: ");


for(i=(m+r);i>=1;i--)
printf("%d ",data[i]);

printf("\n---x---x---x---x---x---x---x---x---x---x---x---x---x---x---x---x---\n");

printf("Enter received codeword: ");


for(i=(m+r);i>=1;i--)//sinceindexing from the right
scanf("%d",&data[i]);

// X1 X2 X3 r bits calculated at receiver side


// Y1 Y2 Y3 r bits calculated at receiver side
// 0 1 0 ->2 if valid code word, XOR = 0 0 0 else it shows position

//initialize an array for the parities for each r bit


int parities[r];
for(i=0;i<r;i++)
{
parities[i]=0;
}

//same for loop as above to count parity


int c=0;
for(i=1;i<=(m+r);i++)
{
if(i==(int)pow(2,c))//c helps keep track of power
{
int count=0; //count number of 1's
for(j=i;j<=(m+r);j++)
{
for(k=0;k<i;k++)
{
if(data[j]==1)
{
count++;
}
j++;
}
j=j+i-1;
}
//????????
if(data[i]==1)
{
count--;
}
//odd parities but they are same????
if(count%2 == data[i])
{
parities[c]=0;
}
//if X Y not equal then they are different so parity 1
else if(count%2 != data[i])
{
parities[c]=1;
}
c++;
}
}

//to find position of error


c=0;
for(int i=0;i<r;i++)
{
c+=parities[i]*((int)pow(2,i));//compute decimal value of parities array
}
//if decimal=0, no error
if(c==0)
{
printf("No Error!\n");
exit(0);
}
//if decimal non 0, error at location c
printf("Error at position : %d\n",c);

//using ternary operator to flip bit


//data[c]= if initially 0, now it'll be 1 else it'll be 0 (as initial value would be 1)
data[c]=data[c]==0 ? 1 : 0;

printf("Code after error correction is: ");


for(i=(m+r);i>=1;i--)//since we reversed
printf("%d ",data[i]);
printf("\n");

return 0;
}

INPUT / OUTPUT:
Hamming code does not work for error in more than 1 bits.

You might also like