Below is the syntax highlighted version of HammingDecoder.java
from §6.1 Data Representations.
/****************************************************************************** * Compilation: javac HammingDecoder.java StdIn.java * Execution: java HammingDecoder < all7.txt * * Read in sequences of 7 Hamming-encoded bits at a time from * stdin, and print 4 corrected bits to stdout. Assumes input * is a multiple of 7 bits (or is terminated with FFFF). * * % java HammingEncoder < all4.txt | java HammingDecoder * ******************************************************************************/ public class HammingDecoder { // return an integer corresponding to the 4 digit hex string public static int fromHex(String s) { return Integer.parseInt(s, 16) & 0xFFFF; } public static void main(String[] args) { while (!StdIn.isEmpty()) { int m1 = StdIn.readInt(); if (m1 == 0xFFFF) break; int m2 = StdIn.readInt(); int m3 = StdIn.readInt(); int m4 = StdIn.readInt(); int p1 = StdIn.readInt(); int p2 = StdIn.readInt(); int p3 = StdIn.readInt(); // check parity bits int c1 = p1 ^ m1 ^ m2 ^ m4; int c2 = p2 ^ m1 ^ m3 ^ m4; int c3 = p3 ^ m2 ^ m3 ^ m4; // flip bits if necessary if (c1 + c2 + c3 == 3) m4 = 1 ^ m4; else if (c1 + c2 == 2) m1 = 1 ^ m1; else if (c1 + c3 == 2) m2 = 1 ^ m2; else if (c2 + c3 == 2) m3 = 1 ^ m3; // print corrected bits StdOut.println(m1 + " " + m2 + " " + m3 + " " + m4); } } }