Sunday, 18 September 2016

Hamming Code: N-bit

Program for N-bit Hamming Code:
import java.util.*;
class hamming{
  public static void main(String sap[]){
    System.out.print("\nNOTE: Example - Format for 7-bit Hamming Code - P1 P2 D3 P4 D5 D6 D7");
    int i,j,k;
    Scanner sc = new Scanner(System.in);

    /*taking no of data bits...*/
    System.out.print("\nEnter no of bits: ");
    int no_of_data_bits = sc.nextInt();

    /*creating parity positions array as per no of data bits...here parity_pos[] array size would be no_of_data_bits and indexing would be from 0 to (no_of_data_bits - 1)...*/
    int parity_pos[] = new int[no_of_data_bits];
    int no_of_parity_bits=0;
    while(no_of_data_bits>Math.pow(2,no_of_parity_bits)){
      parity_pos[no_of_parity_bits] = (int)Math.pow(2,no_of_parity_bits);
      no_of_parity_bits++;
    }
    parity_pos[no_of_parity_bits] = (int)Math.pow(2,no_of_parity_bits);
    no_of_parity_bits++;

    /*creating a sending array of size (no_of_data_bits + no_of_parity_bits + 1)...here send_arr[] indexing would be from 1 to ((no_of_data_bits + no_of_parity_bits + 1) - 1)...index '0' of send_arr[] is not used...*/
    int send_arr[] = new int[no_of_data_bits+no_of_parity_bits+1];

    /*creating a variable 'k' which would be used to bypass the parity positions in send_arr[] by referring parity_pos[]...*/
    k=0;
    System.out.print("\nEnter "+no_of_data_bits+" Data Bits: \n");
    for(j=1;j<send_arr.length;j++){
      if(parity_pos[k]==j){
        k++;
      }
      else{
        /*taking data_bits and storing in send_arr[]...*/
        send_arr[j] = sc.nextInt();
      }
    }

    int result,distance,count;
    /*creating a parity value array as per no_of_data_bits...here indexing of parity_value[] would be from 0 to (no_of_data_bits - 1)...*/
    int parity_value[] = new int[no_of_data_bits];

    /*here variable 'i' would take care of no of parity values to be generated...*/
    i=0;
    while(i<parity_pos.length && parity_pos[i]!=0){
      /*result variable would contain the parity value generated...*/
      result = 0;

      /*here distance and count variable would contain the distance...
      distance would be like...suppose distance = 2 then we would take first 2 values from send_arr[] and skip next 2 values from send_arr[] and repeat the procedure till the end of the send_arr[]...*/
      distance=count=parity_pos[i];

      /*j variable would take care of generating parity as per distance in the send_arr[]...*/
      j=1;
      while(j<send_arr.length){
        /*for this while loop the values of send_arr[] would be considered for generating parity value...and we are not taking care of parity bits as initially parity bits in send_arr[] would be '0'...*/
        while(count<=distance && count>0 && j<send_arr.length){
          result = result + send_arr[j];
          count--;
          j++;
        }

        /*for this while loop the values of send_arr[] would not be responsible for generating parity value...*/
        while(count<distance){
          count++;
          j++;
        }
      }

      /*at the end of every iteration of above while loop...parity value would be generated...and value of parity would be stored in parity_value[]...*/
      parity_value[i] = result%2;
      i++;
    }

    /*merging the send_arr[] and parity_value[]...*/
    j=0;
    for(i=1;i<send_arr.length;i++){
      /*as per parity_pos[] array the values would be inserted respectively in send_arr[]...*/
      if(i==parity_pos[j]){
        send_arr[i] = parity_value[j];
        j++;
      }
    }

    /*Displaying the data to be transmitted...*/
    System.out.print("\nData to be transmitted: ");
    for(i=1;i<send_arr.length;i++){
      System.out.print(send_arr[i]);
    }
    System.out.println();
  }
}

Output:

NOTE: Example - Format for 7-bit Hamming Code - P1 P2 D3 P4 D5 D6 D7
Enter no of bits: 8

Enter 8 Data Bits:
1
0
0
1
1
0
1
0

Data to be transmitted: 011100101010