Computer Networks Laboratory 2023-2024
Experiment 3
Write a program for error detecting code using CRC-CCITT (16 bits).
Theory
CRC(Cyclic Redundancy Check) is an error detecting technique used in digital networks and
storage devices to detect the accidental changes to raw data. It cannot be used for correcting
errors.
If an error is detected in the received message, a „Negative acknowledgement‟ is sent to
the sender. The sender and the receiver agree upon a fixed polynomial called generator
polynomial. The standard agreed generator polynomial is x16+x12+x5+x0 (any polynomial can be
considered, of degree 16).
The CRC does error checking via polynomial division. The generated polynomial g(x) =
x +x +x5+x0
16 12
16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
17 bits.
1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1
So the g(x) value is 10001000000100001
Algorithm:
1. Given a bit string (message to be sent), append 16 0S to the end of it (the number of 0s is
the same as the degree of the generator polynomial) let this string + 0S be called as
modified string B
2. Divide B by agreed on polynomial g(x) and determine the remainder R(x). The 16-bit
remainder received is called as checksum.
3. The message string is appended with checksum and sent to the receiver.
4. At the receiver side, the received message is divided by generator polynomial g(x).
5. If the remainder is 0, the receiver concludes that there is no error occurred otherwise, the
receiver concludes an error occurred and requires a retransmission.
Dept. of ISE, APSCE, Bangalore Page 21
Computer Networks Laboratory 2023-2024
PROGRAM:
import [Link].*;
class crc_gen
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
int[] data;
int[] div;
int[] divisor;
int[] rem;
int[] crc;
int data_bits, divisor_bits, tot_length;
[Link]("Enter number of data bits :");
data_bits=[Link]([Link]());
data=new int[data_bits];
[Link]("Enter data bits : ");
for(int i=0; i<data_bits; i++)
data[i]=[Link]([Link]());
divisor_bits = 17;
divisor = new int[]{1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1};
tot_length=data_bits+divisor_bits-1;
div=new int[tot_length];
rem=new int[tot_length];
crc=new int[tot_length];
/* CRC GENERATION */
for(int i=0;i<[Link];i++)
div[i]=data[i];
[Link]("Dividend (after appending 0's) are : ");
for(int i=0; i< [Link]; i++)
[Link](div[i]);
[Link]();
for(int j=0; j<[Link]; j++){
Dept. of ISE, APSCE, Bangalore Page 22
Computer Networks Laboratory 2023-2024
rem[j] = div[j];
}
rem=divide(divisor, rem);
for(int i=0;i<[Link];i++) //append dividend and ramainder
{
crc[i]=(div[i]^rem[i]);
}
[Link]();
[Link]("CRC code : ");
for(int i=0;i<[Link];i++)
[Link](crc[i]);
/* ERROR DETECTION */
[Link]();
[Link]("Enter CRC code of "+tot_length+" bits :
"); for(int i=0; i<[Link]; i++)
crc[i]=[Link]([Link]());
for(int j=0; j<[Link]; j++){
rem[j] = crc[j];
}
rem=divide(divisor, rem);
for(int i=0; i< [Link]; i++)
{
if(rem[i]!=0)
{
[Link]("Error");
break;
}
if(i==[Link]-1)
[Link]("No Error");
}
[Link]("THANK YOU. .... )");
Dept. of ISE, APSCE, Bangalore Page 23
Computer Networks Laboratory 2023-2024
static int[] divide(int divisor[], int rem[])
{
int cur=0;
while(true)
{
for(int i=0;i<[Link];i++)
rem[cur+i]=(rem[cur+i]^divisor[i]);
while(rem[cur]==0 && cur!=[Link]-1)
cur++;
if(([Link]-cur)<[Link])
break;
}
return rem;
}
}
OUTPUT:
Dept. of ISE, APSCE, Bangalore Page 24
Computer Networks Laboratory 2023-2024
Dept. of ISE, APSCE, Bangalore Page 25