Hamming code Implementation in Java Last Updated : 11 Jun, 2020 Comments Improve Suggest changes 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 Hamming code Implementation in Java A akash2016 Follow Improve Article Tags : Java Computer Networks Java Programs cryptography Practice Tags : Java Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read What is OSI Model? - Layers of OSI Model The OSI (Open Systems Interconnection) Model is a set of rules that explains how different computer systems communicate over a network. OSI Model was developed by the International Organization for Standardization (ISO). The OSI Model consists of 7 layers and each layer has specific functions and re 13 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read TCP/IP Model The TCP/IP model (Transmission Control Protocol/Internet Protocol) is a four-layer networking framework that enables reliable communication between devices over interconnected networks. It provides a standardized set of protocols for transmitting data across interconnected networks, ensuring efficie 7 min read Types of Network Topology Network topology refers to the arrangement of different elements like nodes, links, or devices in a computer network. Common types of network topology include bus, star, ring, mesh, and tree topologies, each with its advantages and disadvantages. In this article, we will discuss different types of n 12 min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Like