1.Write a java program for error detecting code using CRC-CCITT (16- bits).
package error_detecting;
import java.util.Arrays;
import java.util.Scanner;
public class crc {
static int[] input(String x) {
Scanner s = new Scanner(System.in);
System.out.println("Enter no. of " + x + " bits:");
int size = s.nextInt();
int arr[] = new int[size];
System.out.println("Enter " + x + " bits:");
for (int i = 0; i < size; i++)
arr[i] = s.nextInt();
return arr;
}
public static void main(String[] args) {
int[] data, divisor;
int tot_length, i;
Scanner s = new Scanner(System.in);
data = input("data");
divisor = input("divisor");
tot_length = data.length + (divisor.length - 1);
int crc[] = new int[tot_length];
int dividend[] = Arrays.copyOf(data, tot_length);
int reminder[] = Arrays.copyOf(dividend, tot_length);
reminder = divide(divisor, reminder);
for (i = 0; i < dividend.length; i++)
crc[i] = (dividend[i] ^ reminder[i]);
System.out.println("CRC Code: " + Arrays.toString(crc));
System.out.println("Enter CRC code of " + (tot_length) + "bits.");
for (i = 0; i < crc.length; i++) {
reminder[i] = s.nextInt();
}
reminder = divide(divisor, reminder);
System.out.println(Arrays.binarySearch(reminder, 1) > 0 ? "Error" : "NoError");
System.out.println("FINISHED");
}
static int[] divide(int divisor[], int reminder[]) {
int cur = 0;
while (true) {
for (int i = 0; i < divisor.length; i++)
reminder[cur + i] = (reminder[cur + i] ^ divisor[i]);
while (reminder[cur] == 0 && cur != reminder.length - 1)
cur++;
if ((reminder.length - cur) < divisor.length)
break;
}
return reminder;
}
}
**********************OUTPUT 1*********************************
Enter no. of data bits:
10
Enter data bits:
1
1
0
1
0
1
1
0
1
1
Enter no. of divisor bits:
3
Enter divisor bits:
1
1
0
CRC Code: [1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0]
Enter CRC code of 12bits.
1
1
0
1
0
1
1
0
1
1
1
0
NoError
**********************OUTPUT 2*********************************
Enter no. of data bits:
10
Enter data bits:
1
1
0
1
0
1
1
0
1
1
Enter no. of divisor bits:
3
Enter divisor bits:
1
1
0
CRC Code: [1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0]
Enter CRC code of 12bits.
1
1
0
1
1
1
1
0
1
1
1
0
Error