A Hamming Code is a version of forward error correction involving introducing redundancy bits through out the data in order to be able to reconstruct missing bits, in a similar process to a parity check.

Encoding

A given piece of data can be encoded into it’s Hamming Code representing by:

1. Calculating The Size

To make this efficient, we want to know the size of the output data in advance so we can heap allocate it. Hence, we need to first calculate the number of redundancy bits we need, which is given by the formula:

Formula

Where:

  • is the new message length
  • is the length of the input data

We can then solve for the size by adding this new result to the existing string size.

2. Interspersing Redundancy Bits

Next, we allocate a new piece of data where we intersperse a series of parity bits (after deciding if it should be even or odd) at every positions.

Note

The presence of multiple redundancy bits allows to check for burst errors and single-bit errors.

Using powers of two specifically allows us to pinpoint the exact bit where single bit errors occur.

The coverage for each parity bit looks something like:

LabelBitBitBitBitBitBitBit
Position (Decimal)
Position (Binary)
Typedatadatadataparitydataparityparity
Coverage (P1)XXXX
Coverage (P2)XXXX
Coverage (P3)XXXX

This coverage pattern makes it so that every single bit has it’s own unique pattern of failing parity checks, making it easy to identify.