What is Cyclic Redundancy Check?
Cyclic Redundancy Check CRC is an error detection algorithm used in communication networks to check if the transmitted data contains any error.
How CRC works?
CRC generator at sender's end:
1. CRC used a n bit generator polynomial which works as divisor.
Generator = 10101 then n=5
2. Append n-1 number of zeros to the data word.
Data word = 110010101
Appended data word = 110010101 + 0000 = 1100101010000
3. Divide the appended data word by the generator by using binary division.rogram to Simulate Cyclic Redundancy Check (CRC) error detection algorithm for noisy channel.
4. The remainder obtained after division is a n-1 bit CRC code.
Remainder = n-1 bit CRC code =1011
5. Replace the n-1 zeros in data word with the n-1 bit CRC code.
Final data word = 110010101 + 1011 = 1100101011011
6. Transmit the CRC appended data word.
CRC checker at receiver's end:
1. Divide the received data word by the same generator.
2. If the remainder is zero than data is not erroneous else it contains error.
Program:
- C++
- C
- Java
- Python
- C#
- PHP
- Javascript
/*------------------------------------------------
Cyclic Redundancy Check(CRC implementation in C++
-------------------------------------------------*/
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
void calc(int* temp,int* gen,int size)
{
for(int i=0;i<size;i++)
{
if(temp[i]==gen[i])
temp[i]=0;
else
temp[i]=1;
}
}
int main()
{
int *msg, *key;
int msize,ksize,i;
cout<<"\n Eneter the size of key:";
cin>>ksize;
key=new int[ksize];
cout<<"\n Enter key:";
for(i=0;i<ksize;i++)
cin>>key[i];
cout<<"\n Enter the size of message:";
cin>>msize;
msg=new int[msize+ksize-1];
cout<<"\n Enter message:";
for(i=0;i<msize;i++)
cin>>msg[i];
for(i=msize;i<msize+ksize-1;i++)
msg[i]=0;
int *temp=new int[ksize];
int *zkey=new int[ksize];
for(i=0;i<ksize;i++)
{
temp[i]=msg[i]; zkey[i]=0;
}
for(i=ksize-1;i<msize+ksize-1;i++)
{
temp[ksize-1]=msg[i];
if(temp[0]==0)
calc(temp,zkey,ksize);
else
calc(temp,key,ksize);
for(int j=1;j<ksize;j++)
{
temp[j-1]=temp[j];
}
}
cout<<"\n CRC is:";
for(i=0;i<ksize-1;i++)
cout<<temp[i];
for(int i=msize,j=0;i<ksize+msize-1,j<ksize-1;i++,j++)
{
msg[i]=temp[j];
}
int n=rand()%(ksize+msize+5);
msg[n]=!msg[n];
cout<<"\n"<<n<<"\n";
for(i=0;i<ksize;i++)
{
temp[i]=msg[i]; zkey[i]=0;
}
for(i=ksize-1;i<msize+ksize-1;i++)
{
temp[ksize-1]=msg[i];
if(temp[0]==0)
calc(temp,zkey,ksize);
else
calc(temp,key,ksize);
for(int j=1;j<ksize;j++)
{
temp[j-1]=temp[j]; }
}
for(int i=0;i<ksize-1;i++)
{
cout<<temp[i];
}
int flag=1;
for(int i=0;i<ksize-1;i++)
{
if(temp[i]==1)
flag=0;
}
if(flag==0)
cout<<"\nError";
else
cout<<"\nNo error";
getch();
return 0;
}
|
Output
Enter the size of key:5
Enter key:1 0 0 1 1
Enter the size of message:10
Enter message:1 1 0 1 0 1 1 0 1 1
CRC is:1110
1
1111
Error