C Program for Cyclic Redundancy Check : CRC

C Program for Cyclic Redundancy Check : CRC

What is CRC:

A cycle redundancy check (CRC) is an error detecting commonly used in storage devices, etc. Block of data is entered and is checked and it is based on if the remainder is 0 or not and if it not found to be zero then an error is detected in the code.

CRCs are popular because they are simple to implement in binary hardware. CRCs basically are used as xor operation is performed between two numbers if remainder is zero then no error if it’s not zero then error is detected.

Let’s Start writing a C program for CRC

#include<stdio.h>
main()
{
int da[20],di[20],te[20],tem[20],l;
int i,j,m,n,data,div,t,k,e;
clrscr();
printf(“\nEnter the total bit of data and divisor”);
scanf(“%d %d”,&data,&div);
m=data+div-1;
printf(“\nEnter the data:”);
for(i=0;i<data;i++)
{ scanf(“%d”,&da[i]);te[i]=da[i]; }
for(i=data;i<m;i++)
{ te[i]=0; }
printf(“\nEnter the divisor”);
for(i=0;i<div;i++)
{ scanf(“%d”,&di[i]); }
l=div;t=0;
k=0;
for(i=0;i<data;i++)
{
e=0;t=0;
for(j=1;j<div;j++)
{
if(((da[j]==1)&&(di[j]==1))||((da[j]==0)&&(di[j]==0)))
{
tem[j-1]=0;
if(e!=1)
{
k=k+1;
t=t+1;
i=i+1;
}
}
else
{
tem[j-1]=1;
e=1;
}
}
j=0;
for(e=t;e<div-1;e++)
{
da[j]=tem[e];
j++;
}
for(j=j;j<div;j++)
{
if(l>=data+1)
{
da[j]=0;
}
else
{
da[j]=te[l];
l=l+1;
}
}
}
printf(“\n The CRC BITS are\t “);
for(i=0;i<div-1;i++)
{
printf(” %d”,tem[i]);
}
}