Hamming Code program in java
Hamming Code program :
import java.io.*;
import java.util.*;
class Hamming
{
public static void main(String args[])throws IOException
{
Scanner sc=new Scanner(System.in);
int m,h[],n,d[],i=0,j,k,x=0,sum=0,count=0;
System.out.print(“Enter no. of data bits : “);
n=sc.nextInt();
while(n>(Math.pow(2,i)))
{
i++;
}
x=i+1;
m=x+n;
d=new int[n+1];
h=new int[m+1];
System.out.print(“Enter data bits : “);
for(i=1;i<=n;i++)
d[i]=sc.nextInt();
for(i=1,j=0,k=1;k<=m;k++)
{
if (Math.pow(2,j)==k)
{
h[k]=0;j++;
}
else
{
h[k]=d[i];i++;
}
}
for(j=0,k=1;k<=m;k++)
{
if (Math.pow(2,j)==k)
{
h[k]=0; count=0; sum=0;
for(int c=k;c<=m;c++)
{
if(count<k)
{
sum+=h[c];
count++;
}
else
{
c+=k-1;
count=0;
}
}
if(sum%2!=0)
h[k]=1;
else
h[k]=0;
j++;
}}
System.out.print(“Transmitted codeword is : “);
for(i=m;i>=1;i–)
System.out.print(h[i]);
}
}
/*
Hamming Code program in java Output:
Enter no. of data bits : 8
Enter data bits :
1
0
0
1
1
0
1
0
Transmitted codeword is : 010101001110
*/
Download “Hamming Code” Hamming.txt – Downloaded 60 times – 857 B
Leave a Reply
1 Comment on "Hamming Code program in java"
import java.util.Scanner;
class Hamming
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter the number of bits : “);
int n = sc.nextInt();
int a[] = new int[n+1];
int i;
System.out.println(“Enter “+n+” bits one by one : “);
for(i=1;i<=n;i++)
a[i] = sc.nextInt();
int b[] = getCode(a,n);
putParity(b,b.length);
System.out.println("The Hamming Code is : ");
for(i=1;i<b.length;i++)
System.out.print(b[i]+" ");
System.out.println("\nEnter the code received at the end : ");
int c[] = new int[b.length];
for(i=1;i<c.length;i++)
c[i] = sc.nextInt();
System.out.println("Error found at position : "+checkError(c,c.length));
}
public static int[] getCode(int a[],int n)
{
int b[],i=1,j=0,parity=0;
while(i<=n)
{
if(Math.pow(2,parity)==i+parity)
parity++;
else
i++;
}
b=new int[parity+n+1];
int k=1;
for(i=1;i<b.length;i++)
if(Math.pow(2,j)==i)
{
b[i]=0;
j++;
}
else
b[i]=a[k++];
return b;
}
public static void putParity(int b[],int n)
{
int i,j=0;
for(i=1;i<n;i++)
if(Math.pow(2,j)==i)
{
if(checkOnes(i,b,n)%2==1)
b[i]=1;
j++;
}
}
public static int checkOnes(int i,int b[],int n)
{
int k,l,ones=0;
l=i;
k=(2*l);
while(l<n&&k<n)
{
while(l<k)
{
if(b[l]==1)
ones++;
l++;
}
l+=i;
k+=(2*i);
}
while(l<n)
{
if(b[l]==1)
ones++;
l++;
}
return ones;
}
public static int checkError(int c[],int n)
{
int i,j=0,badbits[]=new int[50],bad=0;
for(i=1;i<n;i++)
if(Math.pow(2,j)==i)
{
if(checkOnes(i,c,n)%2==1)
bad+=i;
j++;
}
return bad;
}
}