Advertisement
guitarman0831

Untitled

Jan 19th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.24 KB | None | 0 0
  1. /*
  2.  * Hamming.java
  3.  *
  4.  * Methods to provide the encoding and decoding of bytes using
  5.  * Hamming Code (7,4)
  6.  *
  7.  *
  8.  * I pledge my honor that I have abided by the Stevens Honor System.
  9.  * Jason Ajmo
  10.  *
  11.  * NOTE: Since arrays are indexed from left to right, arr[0] is the MSB and arr[arr.length-1] is the LSB
  12.  *
  13.  * Last updated: 9/23 11:26 PM
  14.  */
  15.  
  16.  import java.util.Scanner;
  17.  
  18.  public class Hamming
  19.  {
  20.     //Instance variable to be accessed across methods
  21.     public static byte unencodedByte;
  22.    
  23.     public static void main(String args[])
  24.     {
  25.         //Varaibles for the user's input, the encoded byte, as well as the scanner
  26.         byte encodedByte;
  27.         int userInput;
  28.         Scanner input = new Scanner(System.in);
  29.        
  30.         //Initialize choice to 1 for sake of compiling
  31.         int choice = 1;
  32.        
  33.         //Continually run until user wants to exit
  34.         do
  35.         {
  36.             //Grab the user's input
  37.             System.out.println("Enter choice:");
  38.             System.out.println("1. Encode");
  39.             System.out.println("2. Decode");
  40.             System.out.println("3. Exit");
  41.        
  42.             choice = input.nextInt();
  43.        
  44.             if(choice == 1)
  45.             {
  46.                 //Read input as integer to store values larger than 127
  47.                 System.out.println("Enter the byte (in integer for) to encode: ");
  48.                 userInput = input.nextInt();
  49.                
  50.                 //Convert to byte, use bitwise AND to ensure that the byte is unsigned
  51.                 unencodedByte = (byte) (userInput & 0xff);
  52.                
  53.                 //Encode it
  54.                 encode(unencodedByte);
  55.             } else if(choice == 2)
  56.             {
  57.                 //Grab the user's input
  58.                 System.out.print("Enter the byte (in integer form) to decode: ");
  59.                 userInput = input.nextInt();
  60.            
  61.                 //Convert to byte
  62.                 encodedByte = (byte) userInput;
  63.            
  64.                 //Decode it and fix any bit flips (only one)
  65.                 decode(encodedByte);
  66.             }
  67.            
  68.         //Only run while the choice is 1 or 2
  69.         } while(choice < 3 && choice > 0);
  70.     }
  71.    
  72.     /*
  73.      * Method to provide encoding of byte
  74.      *
  75.      * @param byte to encode
  76.      * @return nothing
  77.      *
  78.      * Precondition:  unencodedByte is a valid byte
  79.      * Postcondition: unencodedByte is encoded and printed
  80.      */
  81.     public static void encode(byte unencodedByte)
  82.     {  
  83.         //Two int arrays, one for each byte of Hamming Code
  84.         int[] byteOne = new int[8];
  85.         int[] byteTwo = new int[8];
  86.        
  87.         //The total of the two arrays, used to print out the final encoded byte (in int form)
  88.         int totalOne = 0, totalTwo = 0;
  89.        
  90.         //Insert the bits into the respective array
  91.         insertBits(byteOne);
  92.         insertBits(byteTwo);
  93.        
  94.         System.out.println("Encoded byte one: ");
  95.        
  96.         //Go through both arrays to print out sum of contents
  97.         for(int i = 0; i < byteOne.length; i++)
  98.             totalOne += Math.pow(2, 7 - i) * byteOne[i];
  99.        
  100.         for(int i = 0; i < byteTwo.length; i++)
  101.             totalTwo += Math.pow(2, 7 - i) * byteTwo[i];
  102.  
  103.         System.out.println(totalTwo + " " + totalOne);
  104.         System.out.println();
  105.     }
  106.    
  107.     /*
  108.      * Method to provide decoding of byte
  109.      *
  110.      * @param encoded byte to decode
  111.      * @return nothing
  112.      *
  113.      * Precondition:  encodedByte is a valid byte
  114.      * Postcondition: encodedByte is decoded
  115.      */
  116.     public static void decode(byte encodedByte)
  117.     {
  118.         //Array for the bits and variables for the error values
  119.         int[] bits = new int[8];
  120.        
  121.         //int variables to hold the error values
  122.         int e1, e2, e3;
  123.        
  124.         //Put bits into array
  125.         for(int i = 0; i < bits.length; i++)
  126.         {
  127.             bits[7-i] = encodedByte & 0x1;
  128.             encodedByte >>= 1;
  129.         }
  130.        
  131.         //Calculate error values
  132.         e1 = (bits[1] + bits[3] + bits[5] + bits[7]) % 2;
  133.         e2 = (bits[2] + bits[3] + bits[6] + bits[7]) % 2;
  134.         e3 = (bits[4] + bits[5] + bits[6] + bits[7]) % 2;
  135.    
  136.         //Check bit values and perform the respective bit flips if necessary, used else if()
  137.         //because only one flip can be analysed
  138.         if(e1 == 1 && e2 == 1 && e3 == 1)
  139.             bits[7] = (bits[7] == 1) ? 0 : 1;
  140.         else if(e1 == 1 && e3 == 1)
  141.             bits[5] = (bits[5] == 1) ? 0 : 1;
  142.         else if(e2 == 1 && e3 == 1)
  143.             bits[6] = (bits[6] == 1) ? 0 : 1;
  144.         else if(e1 == 1 && e2 == 1)
  145.             bits[3] = (bits[3] == 1) ? 0 : 1;
  146.        
  147.    
  148.         //Print out the decoded byte
  149.         System.out.println("Decoded byte: ");
  150.         System.out.println((int)(bits[3] * Math.pow(2,3) + bits[5] * Math.pow(2,2) + bits[6] * Math.pow(2,1) + bits[7] * Math.pow(2,0)));
  151.         System.out.println();
  152.     }  
  153.    
  154.     /*
  155.      * Additional method to insert bits into array to avoid duplication of code
  156.      *
  157.      * @param valid array to insert bits into
  158.      * @return nothing
  159.      *
  160.      * Precondition:  unencodedByte is set and is at least 4 bits in length, and arr[] is passed in the parameter
  161.      * Postcondition: arr[] is filled with proper data and parity bits from unencodedByte
  162.      *                unencodedByte loses 4 bits
  163.      */
  164.     private static void insertBits(int[] arr)
  165.     {
  166.         //Use byte & 0x1 to calculate LSB; Unsigned RSHIFT by 1 to move bits over
  167.         //to retrieve the next bit
  168.         arr[7] = unencodedByte & 0x1;
  169.         unencodedByte >>= 1;
  170.        
  171.         arr[6] = unencodedByte & 0x1;
  172.         unencodedByte >>= 1;
  173.        
  174.         arr[5] = unencodedByte & 0x1;
  175.         unencodedByte >>= 1;
  176.        
  177.         arr[3] = unencodedByte & 0x1;
  178.         unencodedByte >>= 1;
  179.        
  180.         //Calculate parity bits
  181.         arr[1] = ((arr[3] + arr[5] + arr[7]) % 2 == 0) ? 0 : 1;
  182.         arr[2] = ((arr[3] + arr[6] + arr[7]) % 2 == 0) ? 0 : 1;
  183.         arr[4] = ((arr[5] + arr[6] + arr[7]) % 2 == 0) ? 0 : 1;
  184.        
  185.         //8th bit is to remain unused, store as 0
  186.         arr[0] = 0;
  187.     }
  188.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement