Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Hamming.java
- *
- * Methods to provide the encoding and decoding of bytes using
- * Hamming Code (7,4)
- *
- * Email: [email protected]
- *
- * I pledge my honor that I have abided by the Stevens Honor System.
- * Jason Ajmo
- *
- * NOTE: Since arrays are indexed from left to right, arr[0] is the MSB and arr[arr.length-1] is the LSB
- *
- * Last updated: 9/23 11:26 PM
- */
- import java.util.Scanner;
- public class Hamming
- {
- //Instance variable to be accessed across methods
- public static byte unencodedByte;
- public static void main(String args[])
- {
- //Varaibles for the user's input, the encoded byte, as well as the scanner
- byte encodedByte;
- int userInput;
- Scanner input = new Scanner(System.in);
- //Initialize choice to 1 for sake of compiling
- int choice = 1;
- //Continually run until user wants to exit
- do
- {
- //Grab the user's input
- System.out.println("Enter choice:");
- System.out.println("1. Encode");
- System.out.println("2. Decode");
- System.out.println("3. Exit");
- choice = input.nextInt();
- if(choice == 1)
- {
- //Read input as integer to store values larger than 127
- System.out.println("Enter the byte (in integer for) to encode: ");
- userInput = input.nextInt();
- //Convert to byte, use bitwise AND to ensure that the byte is unsigned
- unencodedByte = (byte) (userInput & 0xff);
- //Encode it
- encode(unencodedByte);
- } else if(choice == 2)
- {
- //Grab the user's input
- System.out.print("Enter the byte (in integer form) to decode: ");
- userInput = input.nextInt();
- //Convert to byte
- encodedByte = (byte) userInput;
- //Decode it and fix any bit flips (only one)
- decode(encodedByte);
- }
- //Only run while the choice is 1 or 2
- } while(choice < 3 && choice > 0);
- }
- /*
- * Method to provide encoding of byte
- *
- * @param byte to encode
- * @return nothing
- *
- * Precondition: unencodedByte is a valid byte
- * Postcondition: unencodedByte is encoded and printed
- */
- public static void encode(byte unencodedByte)
- {
- //Two int arrays, one for each byte of Hamming Code
- int[] byteOne = new int[8];
- int[] byteTwo = new int[8];
- //The total of the two arrays, used to print out the final encoded byte (in int form)
- int totalOne = 0, totalTwo = 0;
- //Insert the bits into the respective array
- insertBits(byteOne);
- insertBits(byteTwo);
- System.out.println("Encoded byte one: ");
- //Go through both arrays to print out sum of contents
- for(int i = 0; i < byteOne.length; i++)
- totalOne += Math.pow(2, 7 - i) * byteOne[i];
- for(int i = 0; i < byteTwo.length; i++)
- totalTwo += Math.pow(2, 7 - i) * byteTwo[i];
- System.out.println(totalTwo + " " + totalOne);
- System.out.println();
- }
- /*
- * Method to provide decoding of byte
- *
- * @param encoded byte to decode
- * @return nothing
- *
- * Precondition: encodedByte is a valid byte
- * Postcondition: encodedByte is decoded
- */
- public static void decode(byte encodedByte)
- {
- //Array for the bits and variables for the error values
- int[] bits = new int[8];
- //int variables to hold the error values
- int e1, e2, e3;
- //Put bits into array
- for(int i = 0; i < bits.length; i++)
- {
- bits[7-i] = encodedByte & 0x1;
- encodedByte >>= 1;
- }
- //Calculate error values
- e1 = (bits[1] + bits[3] + bits[5] + bits[7]) % 2;
- e2 = (bits[2] + bits[3] + bits[6] + bits[7]) % 2;
- e3 = (bits[4] + bits[5] + bits[6] + bits[7]) % 2;
- //Check bit values and perform the respective bit flips if necessary, used else if()
- //because only one flip can be analysed
- if(e1 == 1 && e2 == 1 && e3 == 1)
- bits[7] = (bits[7] == 1) ? 0 : 1;
- else if(e1 == 1 && e3 == 1)
- bits[5] = (bits[5] == 1) ? 0 : 1;
- else if(e2 == 1 && e3 == 1)
- bits[6] = (bits[6] == 1) ? 0 : 1;
- else if(e1 == 1 && e2 == 1)
- bits[3] = (bits[3] == 1) ? 0 : 1;
- //Print out the decoded byte
- System.out.println("Decoded byte: ");
- 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)));
- System.out.println();
- }
- /*
- * Additional method to insert bits into array to avoid duplication of code
- *
- * @param valid array to insert bits into
- * @return nothing
- *
- * Precondition: unencodedByte is set and is at least 4 bits in length, and arr[] is passed in the parameter
- * Postcondition: arr[] is filled with proper data and parity bits from unencodedByte
- * unencodedByte loses 4 bits
- */
- private static void insertBits(int[] arr)
- {
- //Use byte & 0x1 to calculate LSB; Unsigned RSHIFT by 1 to move bits over
- //to retrieve the next bit
- arr[7] = unencodedByte & 0x1;
- unencodedByte >>= 1;
- arr[6] = unencodedByte & 0x1;
- unencodedByte >>= 1;
- arr[5] = unencodedByte & 0x1;
- unencodedByte >>= 1;
- arr[3] = unencodedByte & 0x1;
- unencodedByte >>= 1;
- //Calculate parity bits
- arr[1] = ((arr[3] + arr[5] + arr[7]) % 2 == 0) ? 0 : 1;
- arr[2] = ((arr[3] + arr[6] + arr[7]) % 2 == 0) ? 0 : 1;
- arr[4] = ((arr[5] + arr[6] + arr[7]) % 2 == 0) ? 0 : 1;
- //8th bit is to remain unused, store as 0
- arr[0] = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement