Hamming Code in Java

Hamming code is a special code in a Computer network, which is a set of error-correction codes. In Computer Graphics, it is mainly used for detecting and correcting errors that occur at the time of data transmission from sender to receiver. In Java, we can implement the code to detect and correct errors using Hamming code.

The code is known as Hamming code because it was developed by R.W. Hamming to detect and correct errors.

The redundant bits and parity bits play an important role in detecting and correcting errors. Redundant bits are the extra binary bits that are added to the original data bits, which are ready to transfer from sender to receiver for ensuring that no bits were lost during the data transfer.

Parity bits are the extra bits that are added to the original data(binary bits) so that the total number of 1s is even or odd.

To learn more about Hamming code, redundant bits, and parity bits, go through the section.

General Hamming Code Algorithm

We use the parity bits for identifying and correcting errors, and the process of using parity bits for identifying the error is referred to as Hamming code. These are the following steps for Hamming code:

  1. In the first step, we write the bit positions in binary form. The bit positions should be started from 1(1, 10, 11, 100, etc.).
  2. We mark all those bits as parity bits which are a power of 2 (1, 2, 4, 8, etc.).
  3. We mark all the other bit positions as data bits.
  4. Each data bit is included in a unique set of parity bits, as determined by its bit position in binary form.
    1. All those bits positions whose binary representation have 1 in the 4th position from the least significant bit (8-15, 24-31, 40-47, etc.) are covered by the 8th parity bit.
    2. All those bits positions whose binary representation have 1 in the 3rd position from the least significant bit(4-7, 12-15, 20-23, etc.) are covered by the 4th parity bit.
    3. All those bits positions whose binary representation have 1 in the second position from the least significant bit(2, 3, 6, 7, 10, 11, etc.) are covered by the 2nd parity bit.
    4. All those bits positions whose binary representation have 1 in the least significant position(1, 3, 5, 7, 9, 11, etc.) are covered by the second parity bit.
    5. All the bits, where the bitwise AND of the parity position and the bit position is non-zero, are covered by each parity bit.
  5. Since we check for even parity, set a parity bit to 1 if the total number of ones in the positions it checks is
  6. odd.
  7. Set a parity bit to 0 if the total number of ones in the positions it checks is even.
Hamming Code in Java

Let's implement the logic for Hamming code in Java by taking reference to the above steps:

HammingCodeExample.java

Output:

Hamming Code in Java




Latest Courses