0% found this document useful (0 votes)
35 views7 pages

CRC-16 Error Detection in Java

The document describes a Java program that implements error detection using CRC-16. It takes a binary message, appends 16 zeros, calculates the CRC using a generator polynomial, and checks for errors in the received data. The program outputs the transmitted data and indicates whether an error occurred during transmission.

Uploaded by

yashaswinimc2704
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)
35 views7 pages

CRC-16 Error Detection in Java

The document describes a Java program that implements error detection using CRC-16. It takes a binary message, appends 16 zeros, calculates the CRC using a generator polynomial, and checks for errors in the received data. The program outputs the transmitted data and indicates whether an error occurred during transmission.

Uploaded by

yashaswinimc2704
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

import [Link].

*;

public class CRC

void div(int a[],int k)

int gp[]={1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1};

int count=0; for(int i=0;i<k;i++)

if(a[i]==gp[0])

for(int j=i;j<17+i;j++)

a[j]=a[j]^gp[count++];

count=0;

public static void main(String[] args)

int a[]=new int[100];

int b[]=new int[100];

int len,k;

CRC ob=new CRC();


[Link]("Enter the length of Data Frame:");

Scanner sc=new Scanner([Link]);

len=[Link]();

int flag=0;

[Link]("Enter the Message:");

for(int i=0;i<len;i++)

a[i]=[Link]();

for(int i=0;i<16;i++)

a[len++]=0;

k=len-16;

for(int i=0;i<len;i++)

b[i]=a[i];

[Link](a,k);

for(int i=0;i<len;i++)

a[i]=a[i]^b[i];

[Link]("Data to be transmitted: ");

for(int i=0;i<len;i++)

[Link](a[i]+" ");
}

[Link]();

[Link]("Enter the Received Data: ");

for(int i=0;i<len;i++)

a[i]=[Link]();

[Link](a, k);

for(int i=0;i<len;i++)

if(a[i]!=0)

flag=1;

break;

if(flag==1)

[Link]("error in data");

else

[Link]("no error");

✅ OVERVIEW
The program performs error detection using CRC-16. It does this by:
1. Taking an input data frame (message in bits).
2. Appending 16 zeros (for CRC-16).
3. Using a generator polynomial to divide and compute the CRC bits.
4. XOR-ing the CRC bits back into the original message → forming the transmitted frame.
5. Taking the received frame as input, and rechecking CRC to detect if an error occurred.

✅ CODE BREAKDOWN – STEP BY STEP

📍 STEP 1: Class and div() Method Definition


java
CopyEdit
public class CRC {
void div(int a[], int k) {
int gp[] = {1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1}; // Generator
Polynomial (CRC-16)
int count = 0;

for (int i = 0; i < k; i++) {


if (a[i] == gp[0]) { // If leading bit is 1
for (int j = i; j < 17 + i; j++) { // XOR with generator
a[j] = a[j] ^ gp[count++];
}
count = 0; // Reset count for next XOR
}
}
}
}

🔍 What This Does:


 a[] contains the message + 16 zeros.
 gp[] is the generator polynomial for CRC-16 (17 bits).
 For every bit from 0 to k, if bit is 1, it performs XOR between the generator and the
message.
 This simulates polynomial division over GF(2).

📍 STEP 2: Main Function Begins


java
CopyEdit
public static void main(String[] args) {
int a[] = new int[100]; // Message + CRC storage
int b[] = new int[100]; // Backup for original message
int len, k;
CRC ob = new CRC();
 a[] holds the working bits of the message.
 b[] is a backup to later get the final transmitted message.
 ob is an object of the CRC class to call div().
📍 STEP 3: Taking User Input for Message
java
CopyEdit
[Link]("Enter the length of Data Frame:");
Scanner sc = new Scanner([Link]);
len = [Link]();

[Link]("Enter the Message:");


for (int i = 0; i < len; i++) {
a[i] = [Link](); // Input binary bits (0 or 1)
}

📍 STEP 4: Appending 16 Zeros (for CRC-16)


java
CopyEdit
for (int i = 0; i < 16; i++) {
a[len++] = 0; // Append 16 zeros at the end of message
}
k = len - 16; // Position until original data ends
 CRC-16 uses a 17-bit polynomial → 16 zeros are added as space for remainder.

📍 STEP 5: Backup Original Message


java
CopyEdit
for (int i = 0; i < len; i++) {
b[i] = a[i];
}

📍 STEP 6: Perform CRC Division


java
CopyEdit
[Link](a, k); // Perform division (XOR)
 This modifies array a[], and stores the remainder in the last 16 bits of a[].

📍 STEP 7: XOR Remainder with Original Message


java
CopyEdit
for (int i = 0; i < len; i++) {
a[i] = a[i] ^ b[i];
}
 Now a[] contains original message + CRC checksum → ready to transmit.

📍 STEP 8: Display Transmitted Data


java
CopyEdit
[Link]("Data to be transmitted:");
for (int i = 0; i < len; i++) {
[Link](a[i] + " ");
}

📍 STEP 9: Get Received Data from User


java
CopyEdit
[Link]("\nEnter the Received Data:");
for (int i = 0; i < len; i++) {
a[i] = [Link](); // Simulate received bits
}

📍 STEP 10: Perform CRC Check on Received Data


java
CopyEdit
[Link](a, k); // Perform CRC division on received data

📍 STEP 11: Check if Remainder is Zero


java
CopyEdit
int flag = 0;
for (int i = 0; i < len; i++) {
if (a[i] != 0) {
flag = 1;
break;
}
}
 If any bit ≠ 0 after CRC division, error is detected.

📍 STEP 12: Display Final Result


java
CopyEdit
if (flag == 1)
[Link]("error in data");
else
[Link]("no error");

✅ SAMPLE RUN
Input:
mathematica
CopyEdit
Enter the length of Data Frame:
8
Enter the Message:
1 0 1 1 0 1 0 1
Output:
nginx
CopyEdit
Data to be transmitted:
1 0 1 1 0 1 0 1 ... [16 CRC bits]
Enter the Received Data:
1 0 1 1 0 1 0 1 ... [correct or altered bits]
no error (or) error in data

You might also like