by Neal R. Wagner
Copyright © 2001 by Neal R. Wagner. All rights reserved.
NOTE: This site is obsolete. See book draft (in PDF):
Here is a test of the Hamming mod 11 Error Correcting code, using 3 check digits and 121 digits altogether. The test starts with a random initial word. It first inserts the proper check digits in positions 0, 1 and 11. The test then makes a random change in each position and corrects the change, so there are 121 changes (some are 0 added -- no change). In repeated runs, the code has always corrected the proper position to the old value.
Here is the output from a run:
// H11EC.java: Implement the mod 11 Hamming code public class H11EC { public static int[] inv = {0, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; // Using the sum1 check sum, if have error e and check1 result c, // then pos[e][c] gives the position in error (modulo 11), // using the first check equation. // If the error is e and check11 result is c, // then pos[e][c] gives the value position/11. public static int[][] pos = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {0, 6, 1, 7, 2, 8, 3, 9, 4, 10, 5}, {0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7}, {0, 3, 6, 9, 1, 4, 7, 10, 2, 5, 8}, {0, 9, 7, 5, 3, 1, 10, 8, 6, 4, 2}, {0, 2, 4, 6, 8, 10, 1, 3, 5, 7, 9}, {0, 8, 5, 2, 10, 7, 4, 1, 9, 6, 3}, {0, 7, 3, 10, 6, 2, 9, 5, 1, 8, 4}, {0, 5, 10, 4, 9, 3, 8, 2, 7, 1, 6}, {0, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}}; public static void printArray(int[] a) { for (int i = 0; i < a.length; i++) { if (a[i] == 10) System.out.print("X "); else System.out.print(a[i]); if (i%5 == 0) System.out.print(" "); } System.out.println(); } public static void printUnchecked(int[] a) { System.out.print("? "); // position 0 System.out.print("?"); // position 1 for (int i = 2; i < a.length; i++) { if (i == 11) System.out.print("?"); // position 11 else System.out.print(a[i]); if (i%5 == 0) System.out.print(" "); } System.out.println(); } public static void insertCheck(int[] a) { a[1] = inv[sum1NoCheck(a)]; a[11] = inv[sum11NoCheck(a)]; a[0] = inv[sum0NoCheck(a)]; if (!doCheck(a)) System.out.println("Failure in insertCheck"); } public static boolean doCheck(int[] a) { int error = sum0(a); // amount of error int check1 = sum1(a); int check11 = sum11(a); if (error == 0 && check1 == 0 && check11 == 0) return true; if (error == 0) return false; // a double error int position = pos[error][check11]*11 + pos[error][check1]; if (position >= a.length) { System.out.println("doCheck: position: " + position + ", error: " + error + ", check1: " + check1 + ", check11: " + check11); System.exit(0); } a[position] = (a[position] - error + 11)%11; System.out.println("Position " + position + " corrected to " + a[position]); return true; } public static int sum0(int[] a) { int check = 0; for (int i = 0; i < a.length; i++) check = (check + a[i])%11; return check; } public static int sum0NoCheck(int[] a) { int check = 0; for (int i = 1; i < a.length; i++) check = (check + a[i])%11; return check; } public static int sum1(int[] a) { int check = 0; for (int i = 0; i < a.length; i++) check = (check + (i%11)*a[i])%11; return check; } public static int sum1NoCheck(int[] a) { int check = 0; for (int i = 2; i < a.length; i++) check = (check + (i%11)*a[i])%11; return check; } public static int sum11(int[] a) { int check = 0; for (int i = 0; i < a.length; i++) check = (check + ((i/11)%11)*a[i])%11; return check; } public static int sum11NoCheck(int[] a) { int check = 0; for (int i = 12; i < a.length; i++) check = (check + ((i/11)%11)*a[i])%11; return check; } // main function public static void main (String[] args) { int[] a = new int[121]; boolean checkFlag = false; for (int i = 0; i < a.length; i++) if (i != 11) a[i] = (int)(Math.random() * 10.0); for (int i = 0; i < a.length; i++) { // H11EC.printUnchecked(a); H11EC.insertCheck(a); // H11EC.printArray(a); // System.out.println(H11EC.doCheck(a)); int oldValue = a[i]; a[i] = (a[i] + (int)(Math.random() * 10.0))%10; System.out.print("Position: " + i + " changed from " + oldValue + " to " + a[i] + "; "); if (oldValue == a[i]) System.out.println(); // H11EC.printArray(a); // System.out.println(H11EC.doCheck(a)); H11EC.doCheck(a); if (a[i] != oldValue) System.out.println("**************************"); // H11EC.printArray(a); // System.out.println("******************************"); } } // end of main }
% java H11EC Position: 0 changed from 10 to 3; Position 0 corrected to 10 Position: 1 changed from 10 to 3; Position 1 corrected to 10 Position: 2 changed from 1 to 1; Position: 3 changed from 0 to 2; Position 3 corrected to 0 Position: 4 changed from 0 to 0; . . . (many lines omitted) . . . Position: 117 changed from 4 to 1; Position 117 corrected to 4 Position: 118 changed from 5 to 6; Position 118 corrected to 5 Position: 119 changed from 4 to 9; Position 119 corrected to 4 Position: 120 changed from 7 to 7;