Omni Calculator logo

Hamming Distance Calculator

Created by Davide Borchia
Reviewed by Anna Szczepanek, PhD and Rijk de Wet
Last updated: Jan 18, 2024

With our Hamming distance calculator, you will learn why the distance from 0101 to 1010 is 22 and not 99 β€” at least sometimes!

Are you ready for a short dive into information theory? Here you will learn:

  • What is the Hamming distance?
  • How to calculate the Hamming distance?
  • Where do we apply the Hamming distance?

What is the Hamming distance?

The Hamming distance is a metric used in information theory to measure how much two messages with the same length differ.

For message, we mean a string: whenever you see a number in this article, you should remember that the digits are no more than letters of an alphabet, and you can't perform mathematical operations on them!

πŸ™‹ Forget the definition of distance in a physical way: we will travel in different spaces!

The Hamming distance indicates the number of different digits/letters in a pair of messages. Take the words "Hamming" and "humming": what is the Hamming distance?

Let's see. "Hamming" and "humming" β€” there is one letter of difference, so the Hamming distance is 1.

Increase the Hamming distance to two: farming, humping, camping. As you can see, there are a few examples. If we increase the Hamming distance to three, there is even more: fasting, hosting, hammock or Hamburg.

As you can see, the higher the Hamming distance, the more difficult it is to "see" the original word: that's because we accumulated errors.

Where do we use the Hamming distance?

The Hamming distance is a fundamental concept in the field of error detection and correction. An error in a message is quantifiable by measuring the number of different bits between the corrupted message and the original one.

πŸ”Ž The Hamming distance bears the name of Richard Hamming, a pioneer of the theory of error correction and detection. He introduced both this concept and the framework of Hamming codes in his 1950 work.

The Hamming distance in an error-correcting code defines the capacity of an algorithm to detect and correct an error. The higher the distance, the higher the number of possible messages between two valid codewords, allowing more space to identify the error. You can learn more on this complex topic with our Hamming codes calculator!

How to calculate the Hamming distance?

In computer science, the Hamming distance is usually relegated to strings of numbers: letters lie at a higher level!

We will give you a hint on how to calculate the Hamming distance in a binary and a decimal message.

Take two binary messages with equal length, and write them down:

a=011010100101011b=010110110101001\begin{align*} a=011010100101011\\ b=010110110101001 \end{align*}

Now compare the messages "bitwise", and mark on one of them where the values of the bit in a given position differ between the messages:

a=011010100101011b=010110110101001\begin{align*} a=011010100101011\\ b=01\textcolor{red}{0}\textcolor{red}{1}101\textcolor{red}{1}01010\textcolor{red}{0}1 \end{align*}

Then count the number of bits you found: that is the Hamming distance. For short messages, it doesn't take long to calculate, but on longer ones, it can take a while β€” that's why we made this calculator!

You can apply the same procedure for any other message, in any alphabet. In our Hamming distance calculator, we implemented the decimal system too. Let's see an example of Hamming distance with ten digits!

a=12271995b=02071895\begin{align*} a=12271995\\ b=02071895 \end{align*}

Mark and count where different digits lie in the same position:

a=12271995b=02071895\begin{align*} a=12271995\\ b=\textcolor{red}{0}2\textcolor{red}{0}71\textcolor{red}{8}95 \end{align*}

The Hamming distance in this case is 33. Easy, isn't it?

How to visualize the Hamming distance?

There is a neat way to visualize the Hamming distance β€” it uses geometry to represent the various codewords and find a path connecting them.

This representation can get messy pretty quickly, so don't rely on it to calculate the Hamming distance: writing down the string is usually enough!

Let's start with the simplest of the messages: one binary bit. Our bit can assume a value of either 00 or 11.

Hamming distance visualized (1)
The visualization of the Hamming distance in a single bit message.

Assuming the presence of a corruption, we can visualize the path (in red) that connects the two states. In this case, the path is made of a single segment: the Hamming distance is 11.

We now increase the number of bits to two: how many messages can we build? The answer is four, and we can represent them on a square!

Hamming distance visualized (2)
With an increased number of bits, the complexity increases too.

Let's corrupt two bits: from 0000 we pass to 1111. To connect them, we need to depart from 0000, visit either 0101 or 1010, and arrive at 1111, thus traveling two line segments: the Hamming distance is 22.

We can do it again! Add another bit; which shape will you get?

Hamming distance visualized (3)
Now we have a cube!

A cube, exactly! We created two "levels": in each one, the last two bits are the same as the previous example: the Hamming distance can be at most 22 if you stay on one "floor". Adding another "floor" allows us to increase the maximum distance to 33.

Follow the example highlighted in red above:

010β†’110β†’111β†’101010 \rightarrow 110 \rightarrow 111 \rightarrow 101

We visited two intermediate states and walked three segments. Notice how multiple minimal paths connect the two selected states: in this case, there are 66 of them. Can you find them all?

πŸ”Ž What about paths longer than the maximum Hamming distance? They contain detours! It is impossible to have a Hamming distance 44 in a 33-bits message: you would introduce and then correct an error!

Let's increase the number of bits one last time: now our message is four bits long, and the corresponding shape is a hypercube, a four-dimensional shape that we can represent in our 3D space with two "concentric" cubes. Take a look!

Hamming distance (4)
This is the last time, we swear!

Now we can reach a Hamming distance as big as 44. We chose a single path to show that, but obviously, the number of possible paths is high!

Let's sum the representations:

  • 11 bits: a segment, the maximum Hamming distance is 11;
  • 22 bits: a square, the maximum Hamming distance is 22;
  • 33 bits: a cube, the maximum Hamming distance is 33;
  • 44 bits: a hypercube, the maximum Hamming distance is 44.

Isn't it obvious? You can flip only as many bits as the length of the message to create any other codeword!

How to use our Hamming distance calculator?

There is nothing easier than using our Hamming distance calculator! Choose the numerical system you desire, and input the two messages: we will give you the Hamming distance in the blink of an eye!


What is the Hamming distance between 10101 and 01100?

The Hamming distance between 10101 and 01100 is 3. The two messages differ in positions number 1, 2, and 5. A Hamming distance of 3 on a message with length 5 means that more than half of the digits changed: it is hard to see the original message, as the corruption is pretty high!

How do I measure the Hamming distance?

To calculate the Hamming distance, you simply count the number of bits where two same-length messages differ. An example of Hamming distance 1 is the distance between 1101 and 1001. If you increase the distance to 2, we can give as an example 1001 and 1010.

Is the Hamming distance a metric?

The Hamming distance is a metric (in the mathematical sense) used in error correction theory to measure the distance between two codewords. In detail, the Hamming distance measures the number of different bits in two strings of the same length.

What are distance metrics?

In computer science, distance metrics are a way to measure similarity between data. They don't measure a physical distance between two points in space but the distance of two "codewords" in their relative abstract spaces.

Distance metrics are of great importance in error detection and correction and in machine learning.

Davide Borchia
Related calculators
Numeral system
Message 1
Message 2
Check out 32 similar tech and electronics calculators πŸ’»
3D printing costAmdahl's lawBattery capacity… 29 more
People also viewed…

Black Friday

How to get best deals on Black Friday? The struggle is real, let us help you with this Black Friday calculator!

Helium balloons

Wondering how many helium balloons it would take to lift you up in the air? Try this helium balloons calculator! 🎈

PCB Trace Resistance

The PCB trace resistance calculator is the easiest way to calculate the resistance of any trace in a PCB.

Wind chill

Be prepared for cold weather with the wind chill calculator. Using the air temperature and wind speed, our calculator knows how to calculate wind chill precisely, so you know how cold it's going to feel when you step outside.
Copyright by Omni Calculator sp. z o.o.
Privacy, Cookies & Terms of Service