CN Exp 4
CN Exp 4
CN Exp 4
java
import java.util.*;
class Crc19
{
public static void main(String[] args)
{
int[] data;
int[] div;
int[] divisor;
int[] rem;
int[] crc;
int data_bits,divisor_bits,tot_bits;
Scanner s = new Scanner(System.in);
System.out.println("Enter Number of Data Bits :");
data_bits= s.nextInt();
data= new int[data_bits];
System.out.println("Enter The Data Bits : ");
for(int i=0;i<data_bits;i++)
data[i] = s.nextInt();
System.out.println("Enter The Number of Bits of Divisor");
divisor_bits= s.nextInt();
divisor = new int[divisor_bits];
System.out.println("Enter The Divisor Bits : ");
for(int i=0; i<divisor_bits ; i++)
divisor[i] = s.nextInt();
System.out.println("Data Bits Are :");
for(int i=0; i<data_bits ; i++)
System.out.print(data[i] + " ");
System.out.println("\nDivisor Bits Are :");
for(int i=0; i<divisor_bits; i++)
System.out.print(divisor[i] + " ");
tot_bits = data_bits+divisor_bits-1;
div = new int[tot_bits];
rem = new int[tot_bits];
crc = new int[tot_bits];
for(int i=0;i<data.length;i++)
div[i] = data[i];
System.out.println("\n Dividend after appending zero :");
for(int i=0;i<div.length;i++)
System.out.print(div[i]+" ");
System.out.print("\n");
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("CRC CODE is :");
for(int i=0;i<crc.length;i++)
System.out.print(crc[i] + " ");
System.out.println("\nEnter The CRC CODE :");
for(int i=0;i<crc.length;i++)
crc[i] = s.nextInt();
System.out.println("CRC BITS ARE:");
for(int i =0; i<crc.length ; i++)
System.out.print(crc[i] + " ");
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("\nError in the Code recievd!");
break;
}
if(i==rem.length-1)
System.out.print("\nNo Error in the Code recived!");
}
}
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:
No error:
Error in output:
Checksum:
import java.util.*;
class checksum19
{
static int[] bit1 = new int[8];
static int[] bit2 = new int[8];
static int[] bit3 = new int[8];
static int[] bit4 = new int[8];
static int[] bit5 = new int[8];
static int[] bit0 = {0,0,0,0,0,0,0,1};
static int[] ans = new int[9];
static int[] rec = new int[8];
static int[] rans = new int[8];
static int pcar =0;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Sender's side");
System.out.println("Please Enter 1st 8 Bit Number");
for(int i =0; i<8; i++)
bit1[i] = sc.nextInt();
add(bit1,ans,true);
System.out.println("Please Enter 2nd 8 Bit Number");
for(int i= 0; i<8;i++)
bit2[i] = sc.nextInt();
add(bit2,ans,true);
System.out.println("Please Enter 3rd 8 Bit Number");
for(int i = 0; i<8; i++)
bit3[i] = sc.nextInt();
add(bit3,ans,true);
System.out.println("Please Enter 4th 8 Bit Number");
for(int i=0;i<8;i++)
bit4[i] = sc.nextInt();
add(bit4,ans,true);
System.out.println();
for(int i=0;i<8;i++)
System.out.print(bit1[i] + " ");
System.out.println();
for(int i=0;i<8;i++)
System.out.print(bit2[i] + " ");
System.out.println();
for(int i=0;i<8;i++)
System.out.print(bit3[i] + " ");
System.out.println();
for(int i=0;i<8;i++)
System.out.print(bit4[i] + " ");
System.out.println();
System.out.println("----------------");
for(int i=0;i<8;i++)
System.out.print(ans[i] + " ");
if(pcar == 1)
{
add(bit0,ans,true);
System.out.println();
System.out.print("Wrap sum:");
for(int i=0;i<8;i++)
System.out.print(ans[i] + " ");
}
for(int i = 0; i<8 ; i++)
{
if(ans[i] == 1)
ans[i] = 0;
else
ans[i] = 1;
}
System.out.println();
System.out.print("After 1's complement:");
for(int i =0; i<8;i++)
System.out.print(ans[i] +" ");
pcar = 0;
System.out.println();
System.out.println();
System.out.println("Receiver's side:");
System.out.println("Please Enter 1st 8 Bit Number");
for(int i =0; i<8; i++)
bit1[i] = sc.nextInt();
add(bit1,rans,true);
System.out.println("Please Enter 2nd 8 Bit Number");
for(int i= 0; i<8;i++)
bit2[i] = sc.nextInt();
add(bit2,rans,true);
System.out.println("Please Enter 3rd 8 Bit Number");
for(int i = 0; i<8; i++)
bit3[i] = sc.nextInt();
add(bit3,rans,true);
System.out.println("Please Enter 4th 8 Bit Number");
for(int i=0;i<8;i++)
bit4[i] = sc.nextInt();
add(bit4,rans,true);
System.out.println("Enter The Checksum Code Received :");
for(int i=0;i<8;i++)
bit5[i] = sc.nextInt();
add(bit5,rans,true);
System.out.println();
for(int i=0;i<8;i++)
System.out.print(bit1[i] + " ");
System.out.println();
for(int i=0;i<8;i++)
System.out.print(bit2[i] + " ");
System.out.println();
for(int i=0;i<8;i++)
System.out.print(bit3[i] + " ");
System.out.println();
for(int i=0;i<8;i++)
System.out.print(bit4[i] + " ");
System.out.println();
for(int i=0;i<8;i++)
System.out.print(bit5[i] + " ");
System.out.println();
System.out.println("----------------");
for(int i=0;i<8;i++)
System.out.print(rans[i] + " ");
if(pcar == 1)
{
add(bit0,rans,false);
System.out.println();
System.out.print("Wrap Sum:");
for(int i=0;i<8;i++)
System.out.print(rans[i] + " ");
}
for(int i = 0; i<8; i++)
{
if(rans[i] == 1)
rans[i] = 0;
else
rans[i] = 1;
}
System.out.println();
System.out.print("After 1's complement: ");
for(int i = 0; i<8;i++)
System.out.print(rans[i] +" ");
System.out.println();
if(rans[0]==0 && rans[1]==0 && rans[2]==0&& rans[3]==0 && rans[4]==0
&& rans[5]==0&& rans[6]==0&& rans[7]==0)
{
System.out.println("No Error");
}
else
{
System.out.println("Error");
}
}
static void add(int[] x, int[] ans1,boolean flag )
{
int carry=0;
int temp;
for(int i =7;i>=0;i--)
{
temp = carry+x[i]+ans1[i];
if(temp==2)
{
carry = 1;
ans1[i]= 0;
}
else if(temp==3)
{
ans1[i] = 1;
carry = 1;
}
else if(temp==1)
{
ans1[i] = 1;
carry = 0;
}
else
{
ans1[i] = 0;
carry =0;
}
}
if(carry == 1)
{
pcar++;
carry = 0;
}
if(flag == true)
{
for(int i =0; i<8;i++)
{
ans[i] = ans1[i];
}
}
else
{
for(int i =0; i<8;i++)
{
rec[i] = ans1[i];
}
}
}
}
Output:
Error:
No error:
hamming code :
import java.util.*;
class Hammking19
{
public static void main(String args[])throws Exception
{
Scanner read=new Scanner(System.in);
int i,value;
int a[] = new int[7];
int r[] = new int[7];
int v[] = new int[3];
a[0] = -1;
a[1] = -1;
a[3] = -1;
value = 0;
System.out.println("Enter data in bits:");
for(i=6;i>=0;i--)
if(i!=3 && i!=1 && i!=0)
a[i]=read.nextInt();
int c;
c=0;
if(a[2]==1)
c++;
if(a[4]==1)
c++;
if(a[6]==1)
c++;
if(c%2==0)
a[0]=0;
else
a[0]=1;
c=0;
if(a[2]==1)
c++;
if(a[5]==1)
c++;
if(a[6]==1)
c++;
if(c%2==0)
a[1]=0;
else
a[1]=1;
c=0;
if(a[4]==1)
c++;
if(a[5]==1)
c++;
if(a[6]==1)
c++;
if(c%2==0)
a[3]=0;
else
a[3]=1;
System.out.println("The encoded message is:");
for(i=6;i>=0;i--)
System.out.println(a[i]);
System.out.println();
System.out.println("Received message:");
for(i=6;i>=0;i--)
r[i]=read.nextInt();
c=0;
if(r[0]==1)
c++;
if(r[2]==1)
c++;
if(r[4]==1)
c++;
if(r[6]==1)
c++;
if(c%2==0)
v[0]=0;
else
v[0]=1;
c=0;
if(r[1]==1)
c++;
if(r[2]==1)
c++;
if(r[5]==1)
c++;
if(r[6]==1)
c++;
if(c%2==0)
v[1]=0;
else
v[1]=1;
c=0;
if(r[3]==1)
c++;
if(r[4]==1)
c++;
if(r[5]==1)
c++;
if(r[6]==1)
c++;
if(c%2==0)
v[2]=0;
else
v[2]=1;
value=v[0]+v[1]*2+v[2]*4;
if(value==0)
System.out.println("NO ERROR!!");
else
{
System.out.println("Error detected at: " +value+ "th position");
if(r[value-1]==0)
r[value-1]=1;
else
r[value-1]=0;
System.out.println("Corrected message:");
for(i=6;i>=0;i--)
System.out.println(r[i]);
}
}
}
Output:
error:
No error: