0% found this document useful (0 votes)
24 views5 pages

DCCN

The document provides code for CRC and Hamming code error detection. The CRC code section writes a program that takes in a data and divisor bit string, performs CRC checking on the data, and indicates if there are any errors in the received code word. The Hamming code section writes a program that encodes a 4-bit message into a 7-bit code word using Hamming code principles, and can detect and correct a single bit error in the received code word.

Uploaded by

Harsh Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views5 pages

DCCN

The document provides code for CRC and Hamming code error detection. The CRC code section writes a program that takes in a data and divisor bit string, performs CRC checking on the data, and indicates if there are any errors in the received code word. The Hamming code section writes a program that encodes a 4-bit message into a 7-bit code word using Hamming code principles, and can detect and correct a single bit error in the received code word.

Uploaded by

Harsh Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

EXPERIMENT-3

Write a program for CRC and Hamming code.


Code for CRC

#include<stdio.h>
char data[20],div[20],temp[4],total[100]; int

i,j,datalen,divlen,len,flag=1;

void check(); int

main()
{
printf("Enter the total bit of data:");

scanf("%d",&datalen);

printf("\nEnter the total bit of divisor: ");

scanf("%d",&divlen);

len=datalen+divlen-1; printf("\nEnter

the data:"); scanf("%s",&data); printf("\

nEnter the divisor"); scanf("%s",div);

for(i=0;i<datalen;i++)
{
total[i]=data[i];

temp[i]=data[i];
}
for(i=datalen;i<len;i++)
//padded with zeroes corresponding to divlen total[i]='0';
check(); // check forcrc

for(i=0;i<divlen;i++)// append crc output (remainder) at end of temp

temp[i+datalen]=data[i];
printf("\ntransmitted Code Word:%s",temp);
printf("\n\nEnter the received code word:");
scanf("%s",total);
check();
for(i=0;i<divlen-1;i++)
if(data[i]=='1')
{
flag=0; break;
}
if(flag==1)
printf("\nsuccessful!!");
else
printf("\nreceived code word contains errors...\n");
}
void check()
{
for(j=0;j<divlen;j++)
data[j]=total[j];
while(j<=len)
{
if(data[0]=='1')
// in XOR ans remains as it is except in case of 1
for(i = 1;i <divlen ; i++)
data[i] = (( data[i] == div[i])?'0':'1');
for(i=0;i<divlen-1;i++)
data[i]=data[i+1];
data[i]=total[j++];
// replace empty right by total
}
}

OUTPUT:

Hamming code:
#include<stdio.h>

#include<conio.h>

void main() {

int data[7],rec[7],i,c1,c2,c3,c;

printf("this works for message of 4bits in size\nenter message bit


onebyone: ");

scanf("%d%d%d%d",&data[0],&data[1],&data[2],&data[4]);
data[6]=data[0]^data[2]^data[4];

data[5]=data[0]^data[1]^data[4];
data[3]=data[0]^data[1]^data[2];
printf("\nthe encoded bits are given below: \n");
for (i=0;i<7;i++)
{
printf("%d ",data[i]);
}

printf("\nenter the received data bits one by one: ");


for (i=0;i<7;i++)
{
scanf("%d",&rec[i]);
}
c1=rec[6]^rec[4]^rec[2]^rec[0];
c2=rec[5]^rec[4]^rec[1]^rec[0];
c3=rec[3]^rec[2]^rec[1]^rec[0];
c=c3*4+c2*2+c1 ;

if(c==0)
{
printf("\ncongratulations there is no error: ");
}

else
{
printf("\nerron on the postion: %d\nthe correct message is \n",c);
if(rec[7-c]==0)
rec[7-c]=1; else rec[7-c]=0;
for (i=0;i<7;i++)
{
printf("%d ",rec[i]);
}
}
getch();
}

OUTPUT:

You might also like