Upcoming SlideShare
×

# CRC JAVA CODE

10,493 views

Published on

1 Comment
3 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• it is very useful

Are you sure you want to  Yes  No
Views
Total views
10,493
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
289
1
Likes
3
Embeds 0
No embeds

No notes for slide

### CRC JAVA CODE

1. 1. OOSPPROJECTSubmitted To : Mr. Amol VasudevaSubmitted By: Sandeep Kumar101026X-1(ECE)JAVA Code: Cyclic RedundancyCheck for Error-Detection
2. 2. Cyclic Redundancy Check Code for Error- DetectionThe Cyclic Redundancy Check (CRC) is a technique for detecting errors in datatransmission, but not for correcting errors when they are detected.Algorithm:(A) For Computing CRC : The CRC Algorithm is based on polynomial arithmetic. Let the message that we have to send has k bits(denoted by M(x) in polynomialform having degree (k-1) )The sender and the receiver are agreed upon agenerator polynomial having r bits (denoted by G(x) in polynomial formhaving degree(r-1) ). The generator polynomial is also called “Divisor”. Now, append (r-1) zero bits to the LSB side of the message M(x) so it will nowcontain (k+r-1) bits and corresponds to the polynomial x(r-1)M(x). Divide the polynomial x(r-1)M(x) by Divisor, using modulo-2 subtraction(bit bybit XOR operation).Add the remainder R(x)(called frame check sequence) tox(r-1)M(x), using modulo-2 addition (bit by bit XOR operation). This is themessage that will be transmitted by the transmitter denoted by T(x).(B) For Error Detection: Suppose that a transmission error occurs , so that the received message at thereceiver is T(x)+E(x), instead of T(x).Each 1 bit in E(x) corresponds to a bit thathas been inverted. The received message at the receiver end is divided by G(x), i. e. [T(x)+E(x)/G(x)]. Since T(x) /G(x) is 0, so the result is simply E(x)/G(x). If E(x)/G(x)=0 than there is no error in the received message, otherwise there isan error. The following type of errors can be detected using CRC: If G(x) has more than one bit and the coefficient of x0is 1, then all singlebit errors are detected. If G(x) is not divisible by x (the coefficient of x0is 1), and t is the leastpositive integer(0<t<n-1) such that G(x) divides xt+1, then all isolateddouble errors are detected. If G(x) has a factor (x+1), then all odd numbered errors are detected.
3. 3. Some standard Generator Polynomials are shown below:Name Generator PolynomialCRC-8 x8+x2+x+1CRC-10 x10+x9+x5+x4+x2+1CRC-16 x16+x12+x5+1CRC-32 x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1The following figure illustrates the computation of CRC
4. 4. CODE:import java.io.*;class crc{public static void main(String a[]) throws IOException{InputStreamReader isr = new InputStreamReader(System.in);BufferedReader br = new BufferedReader(isr);int[] message;int[] gen;int[] app_message;int[] rem;int[] trans_message;int message_bits,gen_bits, total_bits;System.out.println("n Enter number of bits in message : ");message_bits=Integer.parseInt(br.readLine());message=new int[message_bits];System.out.println("n Enter message bits : ");for(int i=0; i<message_bits; i++)message[i]=Integer.parseInt(br.readLine());System.out.println("n Enter number of bits in gen : ");gen_bits=Integer.parseInt(br.readLine());gen=new int[gen_bits];System.out.println("n Enter gen bits : ");for(int i=0; i<gen_bits; i++){gen[i]=Integer.parseInt(br.readLine());}total_bits=message_bits+gen_bits-1;app_message=new int[total_bits];rem=new int[total_bits];trans_message=new int[total_bits];for(int i=0;i<message.length;i++){app_message[i]=message[i];}System.out.print("n Message bits are : ");for(int i=0; i< message_bits; i++){System.out.print(message[i]);}
5. 5. System.out.print("n Generators bits are : ");for(int i=0; i< gen_bits; i++){System.out.print(gen[i]);}System.out.print("n Appended message is : ");for(int i=0; i< app_message.length; i++){System.out.print(app_message[i]);}for(int j=0; j<app_message.length; j++){rem[j] = app_message[j];}rem=computecrc(app_message, gen, rem);for(int i=0;i<app_message.length;i++){trans_message[i]=(app_message[i]^rem[i]);}System.out.println("n Transmitted message from the transmitter is : ");for(int i=0;i<trans_message.length;i++){System.out.print(trans_message[i]);}System.out.println("n Enter received message of "+total_bits+" bits at receiver end : ");for(int i=0; i<trans_message.length; i++){trans_message[i]=Integer.parseInt(br.readLine());}System.out.println("n Received message is :");for(int i=0; i< trans_message.length; i++){System.out.print(trans_message[i]);}for(int j=0; j<trans_message.length; j++){rem[j] = trans_message[j];}rem=computecrc(trans_message, gen, rem);for(int i=0; i< rem.length; i++){if(rem[i]!=0)
6. 6. {System.out.println("n There is Error in the received message!!!");break;}if(i==rem.length-1){System.out.println("n There is No Error in the received message!!!");}}static int[] computecrc(int app_message[],int gen[], int rem[]){int current=0;while(true){for(int i=0;i<gen.length;i++){rem[current+i]=(rem[current+i]^gen[i]);}while(rem[current]==0 && current!=rem.length-1){current++;}if((rem.length-current)<gen.length){break;}}return rem;}}OUTPUT: