## 1. Introduction

Error detection and correction are crucial to ensure reliable data transmission between devices. One of the most widely used error detection and correction methods is Hamming code. Richard Hamming developed it in 1950 at Bell Labs. In particular, it can detect and correct a single-bit error by adding redundant (or parity) bits to the original data.

A few use cases of Hamming code include:

• Computer memory systems: RAM and hard disk
• Communication networks: Bluetooth, ethernet, and wi-fi
• Digital signal processing: Satellite communications and digital audio broadcasting

In this tutorial, we’ll first study the basic terms of Hamming code, such as redundant bits, parity bits, and parity bit calculation. Next, we’ll provide an illustrative example of how to encode a data block using the Hamming code. Then, we’ll explore the various steps involved in error detection and correction. After that, we’ll discuss the advantages and limitations of the Hamming code. Finally, we’ll wrap up the tutorial with a brief summary of the topics covered.

## 2. Hamming Code Basics

In this section, we’ll discuss the fundamental terminologies of the Hamming code, including redundant bits, parity bits, and parity bit calculation.

### 2.1. Redundant Bits

The central idea of the Hamming code is based on redundancy. In general, the sender first divides the original data into fixed-sized blocks. It then adds some extra redundant bits to each data block before transmitting the block to the receiver. Upon reception, the receiver uses these bits to detect transmission error, locate the corrupted bit, and correct it. We can calculate the required number of redundant bits using the equation: In this equation, is the number of redundant bits, and is the number of data bits in a block.

### 2.2. Parity Bits

We add parity bits as redundant bits to an original data block for detecting and correcting errors during transmission. These bits ensure that each data block’s total count of is either even or odd. There are two types of parity bits:

Odd parity bit: We set a parity bit to when the count of in a bit sequence is even; otherwise, we configure it to .

Even parity bit: We fix a parity bit to if the count of in a given set of bits is odd; otherwise, we set it to .

### 2.3. Parity Bit Calculation

A Hamming code has number of bits, which is the sum of data bits and parity bits. We now discuss the step-by-step process to obtain the position and value of parity bits:

1. At first, we compute the number of parity bits using and the total number of bits in the Hamming code using 2. Next, we convert the bit positions from to into binary numbers, i.e., 3. Then, we label the bit positions corresponding to the power of as parity bits and the remaining positions as the data bits, e.g., 4. At last, we determine the value of each parity bit in the Hamming code iteratively. In each iteration, we determine the value of parity bit by counting the number of in the bit positions that have in the position from the least significant bit in their binary representations. Then, we set the parity bit to if the number of is odd, considering even parity; otherwise, we set it to ## 3. An Illustrative Example

Let’s break down the process of generating a Hamming code by looking at an example binary data block . This will help us understand how the Hamming code works and how it detects and corrects errors during data transmission. We’ll now apply each of the steps discussed in the previous section, one by one.

Step 1: The number of parity bits is since and . Therefore, the length of the Hamming code is , that is, .

Step 2: The binary conversion of the bit positions from to is .

Step 3: The parity bits are placed at the positions since they are power of . In contrast, the data bits are kept at the positions . The image below shows the labeling of parity and data bits:

Step 4 – iteration 1: In the first iteration, we calculate the value of parity bit by counting the number of at the positions . These positions are chosen because their first bit from the least significant bit is equal to 1 in their binary representations. It is illustrated by: We consider even parity and set the parity bit since the count of at the positions is . The updated code is: Step 4 – iteration 2: Next, the value of parity bit is calculated based on the number of at the positions . This is because the second bit from the least significant bit of these positions are 1 in their binary representations:

## We then assign because there are occurrences of at the positions . The new code is: Step 4 – iteration 3: In the third iteration, we calculate the value of parity bit . To do so, we first count the number of at the positions . The reason is that the third bit from the least significant bit in the binary representation of these positions is . It can be seen in the image below.

## Next, we set the value of parity bit because we count instances of at the positions . Now, the code is: Step 4 – iteration 4: We finally count the number of at the positions to finalize the value of parity bit . This is because the fourth bit from the least significant bit is equal to 1 in the binary representation of these positions: Hence, we set the parity bit since the count of at the positions is . The final Hamming code is: ## 4. Error Detection and Correction

Let’s first discuss how to detect if there is an error in the received code. To do so, we recalculate the values of parity bits and perform parity checks. In particular, we record a for a parity check if the assigned and observed parity bits do not match. Otherwise, we record a . The recorded values are then written from right to left to form the checking number. The code is error-free if the checking number is zero. Otherwise, an error has occurred at the bit position corresponding to the checking number, given that there is only a single-bit error.

Let’s assume that the eleventh bit of the codeword generated in the previous section is changed from to during transmission. Then, the received code is: Now, we recalculate the parity bits and record their values based on the assigned and observed parity bits. The images below show the recorded values for parity bits , , , and , respectively:    Let’s obtain the checking number by writing the recorded values from right to left, i.e., , which is equal to decimal number eleven. This signifies that the received code has an error at the eleventh bit. In order to correct it, we just have to toggle the eleventh bit and the corrected code is: ` `