Hamming code Implementation in Java Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Pre-requisite: Hamming code Hamming code is a set of error-correction codes that can be used to detect and correct the errors that can occur when the data is moved or stored from the sender to the receiver. It is a technique developed by R.W. Hamming for error correction. Examples: Input: message bit = 0101 r1 r2 m1 r4 m2 m3 m4 0 1 0 1 Output: Generated codeword : r1 r2 m1 r4 m2 m3 m4 0 1 0 0 1 0 1 Explanation: Initially r1, r2, r4 is set to '0'. r1 = xor of all bits position which has '1' in its 0th-bit position r2 = xor of all bits which has '1' in its 1st-bit position r3 = xor of all bits which has '1' in its 2nd-bit position Below is the implementation of the Hamming Code: Java // Java code to implement Hamming Code class HammingCode { // print elements of array static void print(int ar[]) { for (int i = 1; i < ar.length; i++) { System.out.print(ar[i]); } System.out.println(); } // calculating value of redundant bits static int[] calculation(int[] ar, int r) { for (int i = 0; i < r; i++) { int x = (int)Math.pow(2, i); for (int j = 1; j < ar.length; j++) { if (((j >> i) & 1) == 1) { if (x != j) ar[x] = ar[x] ^ ar[j]; } } System.out.println("r" + x + " = " + ar[x]); } return ar; } static int[] generateCode(String str, int M, int r) { int[] ar = new int[r + M + 1]; int j = 0; for (int i = 1; i < ar.length; i++) { if ((Math.ceil(Math.log(i) / Math.log(2)) - Math.floor(Math.log(i) / Math.log(2))) == 0) { // if i == 2^n for n in (0, 1, 2, .....) // then ar[i]=0 // codeword[i] = 0 ---- // redundant bits are initialized // with value 0 ar[i] = 0; } else { // codeword[i] = dataword[j] ar[i] = (int)(str.charAt(j) - '0'); j++; } } return ar; } // Driver code public static void main(String[] args) { // input message String str = "0101"; int M = str.length(); int r = 1; while (Math.pow(2, r) < (M + r + 1)) { // r is number of redundant bits r++; } int[] ar = generateCode(str, M, r); System.out.println("Generated hamming code "); ar = calculation(ar, r); print(ar); } } Output: Generated hamming code r1 = 0 r2 = 1 r4 = 0 0100101 Comment More infoAdvertise with us Next Article CompoundName get() method in Java with Examples A akash2016 Follow Improve Article Tags : Java cryptography Practice Tags : Java Similar Reads Coding Guidelines in Java Java is one of the most popular and widely used programming languages and platforms. A platform is an environment that helps to develop and run programs written in any programming language. Java is fast, reliable, and secure. From desktop to web applications, scientific supercomputers to gaming cons 7 min read Execution Engine in Java Java virtual machine or JVM can be visualized as a virtual machine residing in the computer that provides an environment for the code to get executed. Java Runtime Environment or JRE is an implementation of the JVM. In order to execute the code, an execution engine is used. In this article, let's un 3 min read Closures in Java with Examples A method is a collection of statements that perform some specific task and return the result to the caller. A method can perform some specific task without returning anything. Methods allow us to reuse the code without retyping the code. In Java, every method must be part of some class that is diffe 5 min read Compilation and Execution of a Java Program Java, being a platform-independent programming language, doesn't work on the one-step compilation. Instead, it involves a two-step execution, first through an OS-independent compiler; and second, in a virtual machine (JVM) which is custom-built for every operating system. The two principal stages ar 5 min read CompoundName get() method in Java with Examples The get() method of a javax.naming.CompoundName class is used to get a component of this compound name object. The position passed as a parameter used to get the component present at that position from the compound name object. Syntax: public String get(int posn) Parameters: This method accepts posn 2 min read Java 11 - Features and Comparison Every 6 months, Oracle releases new Java versions. The September Java 11 version is already making a lot of buzz in the computer science world even before it was released by declaring that from now on Java is gonna be paid for commercial use. The following articles will show important features, enha 5 min read Like