Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- input : data bits
- outpot : hamming code
- Description : b= number of bits in our case it is 7
- r= number of parity bits i.e. p1 p2 p3 p4
- hb= number of bits in hamming code in our case (b+r) = 11
- d[]= Array containing data bits i.e. input
- h[]= Array containing hamming code i.e. output
- COUNT= is static counter for counting number of 1's
- */
- import java.io.*;
- class Hamming_Gen
- {
- static int COUNT,K;
- public static void main(String[] args) throws IOException
- {
- int r,b,hb;
- BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
- System.out.println("Enter number of bits : ");
- b=Integer.parseInt(br.readLine());
- int d[]= new int[b+1];
- for(r=1; ;r++)
- {
- if((int) Math.pow(2,r) >= (b+r+1))
- break;
- }
- hb=(b+r+1);
- System.out.println("Number of parity bits : "+r);
- System.out.println("Number of hamming bits : "+hb);
- int h[]= new int[hb];
- int p[]=new int[r];
- int location[][] = new int [r][hb];
- System.out.println("Enter bit number : ");
- for(int i=1;i<=b;i++)
- d[i]=Integer.parseInt(br.readLine());
- System.out.println("Data bits are : ");
- for(int i=1;i<=b;i++)
- System.out.print(d[i]);
- System.out.println();
- hamming_array_generation(d,h,r,hb);
- show_hamming_code(h,hb);
- System.out.println("Locations");
- for(int i=0;i<r;i++)
- get_Locations(i,h,location,p);
- for (int i=0; i<location.length; i++) {
- for (int j=0; j<location[i].length; j++) {
- System.out.print(" "+location[i][j]);
- }
- System.out.println();
- }
- Generate_parity(location, p, h);
- for(int i=0;i<r;i++)
- System.out.print(" "+p[i]);
- show_hamming_code(h,hb);
- System.out.println("Enter hamming code of "+(hb-1)+" bits : ");
- for(int i=1;i<hb;i++)
- h[i]=Integer.parseInt(br.readLine());
- error_calculate(location,h,p,r);
- show_hamming_code(h,hb);
- System.out.println("THANK YOU!!......ufff");
- }
- static void hamming_array_generation(int d[],int h[],int r,int hb)
- {
- int i=1,j=0,k=1;
- while(i<hb || j<r)
- {
- if((int)Math.pow(2,j) == i)
- {
- h[i]=0;
- j++;
- }
- else
- {
- h[i]=d[k];
- k++;
- }
- i++;
- }
- }
- static void get_Locations(int i,int h[], int location[][], int p[])
- {
- K=0;
- int a,add,num,b,count=0;
- num=(int)Math.pow(2,i);
- add=num+num;
- a=num;
- while(a<=11)
- {
- calculate(a,h,i,location);
- if(num>1)
- {
- b=a;
- for(int k=1;k<num;k++)
- {
- b=b+1;
- if(b<=11)
- {
- calculate(b,h,i,location);
- }
- else
- break;
- }
- }
- a=a+add;
- }
- //COUNT=0;
- }
- static void calculate(int a, int h[], int i, int location[][])
- {
- if((int)Math.pow(2,i) == a)
- return;
- location[i][K] = h[a];
- K++;
- }
- static void Generate_parity(int location[][], int p[], int h[])
- {
- int count=0;
- for (int i=0; i<location.length; i++) {
- for (int j=0; j<location[i].length; j++) {
- if(location[i][j] == 1)
- count++;
- }
- int k=(int)Math.pow(2,i);
- if(count%2 == 0)
- {
- p[i]=0;
- h[k]=0;
- }
- else
- {
- p[i]=1;
- h[k]=1;
- }
- count=0;
- }
- }
- static void error_calculate(int location[][], int h[], int p[], int r)
- {
- int k=0,d=0;
- for(int i=0;i<r;i++)
- {
- k=(int)Math.pow(2,i);
- p[i]=h[k];
- }
- int count=0;
- for (int i=0; i<location.length; i++) {
- for (int j=0; j<location[i].length; j++) {
- if(location[i][j] == 1)
- count++;
- }
- k=(int)Math.pow(2,i);
- if((count%2 == 0 && h[k]==0)||(count%2 != 0 && h[k]==1))
- {
- p[i]=0;
- }
- else
- {
- p[i]=1;
- }
- count=0;
- }
- for(int i=3;i>=0;i--)
- d=d+(p[i]*((int)Math.pow(2,i)));
- if(d==0)
- System.out.println("No Error");
- else
- {
- System.out.println("The "+d+" bit is wrongly received");
- if(h[d]==0)
- h[d]=1;
- else
- h[d]=0;
- System.out.println("");
- System.out.println("The correct hamming code is ");
- }
- }
- static void show_hamming_code(int h[],int hb)
- {
- System.out.println("HAMMING CODE ARRAY : ");
- for(int i=1; i<hb; i++)
- {
- System.out.print(h[i]);
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement