Monday, 14 August 2017

Error Detection (crc.java) (CN)

//Aim: Write a program for Error Detection usinng CRC.
/*For complete explanation visit: Prof. Brinda's Blog*/
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class crc { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int[] data; int[] div; int[] divisor; int[] rem; int[] crc; int data_bits, divisor_bits, tot_length; System.out.println("Enter number of data bits : "); data_bits=Integer.parseInt(br.readLine()); data=new int[data_bits]; System.out.println("Enter data bits : "); for(int i=0; i<data_bits; i++) data[i]=Integer.parseInt(br.readLine()); System.out.println("Enter number of bits in divisor : "); divisor_bits=Integer.parseInt(br.readLine()); divisor=new int[divisor_bits]; System.out.println("Enter Divisor bits : "); for(int i=0; i<divisor_bits; i++) divisor[i]=Integer.parseInt(br.readLine()); // Display the Data and Divisor Bits System.out.print("\n Data bits are : "); for(int i=0; i< data_bits; i++) System.out.print(data[i]); System.out.print("\n Divisor bits are : "); for(int i=0; i< divisor_bits; i++) System.out.print(divisor[i]); tot_length=data_bits+divisor_bits-1; // Declaration div=new int[tot_length]; rem=new int[tot_length]; crc=new int[tot_length]; /*------------------ CRC GENERATION-----------------------*/ for(int i=0;i<data.length;i++) div[i]=data[i]; System.out.print("\n Dividend (after appending 0's) are : "); for(int i=0; i< div.length; i++) System.out.print(div[i]); System.out.println(); for(int j=0; j<div.length; j++) { rem[j] = div[j]; } rem=divide(div, divisor, rem); for(int i=0;i<div.length;i++) //append dividend and ramainder { crc[i]=(div[i]^rem[i]); } System.out.println(); System.out.println("CRC code : "); for(int i=0;i<crc.length;i++) System.out.print(crc[i]); /*-------------------ERROR DETECTION---------------------*/ System.out.println(); System.out.println("Enter CRC code of "+tot_length+" bits : "); for(int i=0; i<crc.length; i++) crc[i]=Integer.parseInt(br.readLine()); for(int j=0; j<crc.length; j++) { rem[j] = crc[j]; } rem=divide(crc, divisor, rem); for(int i=0; i< rem.length; i++) { if(rem[i]!=0) { System.out.println("Error"); break; } if(i==rem.length-1) System.out.println("No Error"); } } static int[] divide(int div[],int divisor[], int rem[]) { int cur=0; while(true) { for(int i=0;i<divisor.length;i++) rem[cur+i]=(rem[cur+i]^divisor[i]); while(rem[cur]==0 && cur!=rem.length-1) cur++; if((rem.length-cur)<divisor.length) break; } return rem; } } Output CRC WITH ERROR Enter number of data bits : 5 Enter data bits : 1 1 0 0 1 Enter number of bits in divisor : 3 Enter Divisor bits : 1 1 0 Data bits are : 11001 Divisor bits are : 110 Dividend (after appending 0's) are : 1100100 CRC code : 1100110 Enter CRC code of 7 bits : 1 0 0 0 1 1 0 Error CRC WITHOUT ERROR Enter number of data bits : 5 Enter data bits : 1 1 0 0 1 Enter number of bits in divisor : 3 Enter Divisor bits : 1 1 0 Data bits are : 11001 Divisor bits are : 110 Dividend (after appending 0's) are : 1100100 CRC code : 1100110 Enter CRC code of 7 bits : 1 1 0 0 1 1 0 No Error

No comments:

Post a Comment