Import Public Class Public Static Void Throws New New Int Int Int Int Int Int

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

import java.io.

*;
public class crcgen {
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 the number of 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 no bit indivision");
divisor_bits=Integer.parseInt(br.readLine());
divisor=new int[divisor_bits];
System.out.println("enter division bits");
for(int i=0;i<divisor_bits;i++)
divisor[i]=Integer.parseInt(br.readLine());
System.out.print("data bits are");
for(int i=0;i<data_bits;i++)
System.out.print(data[i]);
System.out.println();
System.out.print("division bits are");
for(int i=0;i<divisor_bits;i++)
System.out.print(divisor[i]);
System.out.println();
tot_length=data_bits+divisor_bits-1;
div=new int[tot_length];
rem=new int[tot_length];
crc=new int[tot_length];
for(int i=0;i<data.length;i++)
div[i]=data[i];
System.out.print("divided(after appending 0's) are");
for(int i=0;i<div.length;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++)
{
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]);
System.out.println();
System.out.println("enter crc code of"+"bits");
for(int i=0;i<crc.length;i++)
System.out.println(crc[i]);
System.out.println();
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");
}
System.out.println("THANK YOU.....");
}
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;
}
}

You might also like