The Richard Hamming, hamming distance calculator is a powerful tool used in information theory and coding to measure the divergence between two binary strings.
Consider the following example:
- Binary String 1: 1010
- Binary String 2: 1100
To calculate the Hamming distance, we compare each bit:
- First bit: 1 vs 1 (match)
- Second bit: 0 vs 1 (differ)
- Third bit: 1 vs 0 (differ)
- Fourth bit: 0 vs 0 (match)
The Hamming distance in this case is 2, as there are two positions where the bits differ.
Hamming Distance Calculator
Binary String A | Binary String B | Hamming Distance |
---|---|---|
1100110 | 1010101 | 3 |
11110000 | 00001111 | 8 |
10101010 | 01010101 | 8 |
11001100 | 11110000 | 4 |
10001 | 10011 | 2 |
00000000 | 11111111 | 8 |
11111111 | 00000000 | 8 |
10111010 | 10010011 | 4 |
00110011 | 11001100 | 4 |
01010101 | 10101010 | 8 |
11100011 | 11000101 | 3 |
01101100 | 10111010 | 5 |
00101010 | 00100001 | 2 |
11010101 | 11100111 | 4 |
10011001 | 01100110 | 6 |
Hamming Distance Calculation Formula
For two binary strings A and B of equal length, the Hamming distance H(A, B) is defined as:
H(A, B) = Σ(A[i] ⊕ B[i])
Where:
- Σ represents the sum
- ⊕ denotes the XOR operation
- i ranges from 1 to the length of the strings
This formula counts the number of ‘1’s in the XOR result of the two strings.
Let’s break it down with an example:
- A: 1011
- B: 1101
Perform XOR: 1011 ⊕ 1101 = 0110
Count ‘1’s in the result: The result has 2 ‘1’s.
The Hamming distance between 1011 and 1101 is 2.
How is Hamming distance calculated?
Calculating Hamming distance involves a bit-by-bit comparison of two binary strings. Here’s a step-by-step process:
- Ensure both strings are of equal length.
- Initialize a counter to zero.
- Compare corresponding bits from left to right.
- For each position where bits differ, increment the counter.
- The final counter value is the Hamming distance.
Let’s apply this to an example:
String 1: 10110
String 2: 11001
Comparison:
1|0|1|1|0
1|1|0|0|1
X| |X|X|X
Counting these, we get a Hamming distance of 4.
What is the Hamming distance between 11111000 and 10000111?
Let’s analyze this pair of 8-bit strings:
11111000
10000111
Comparing bit by bit:
- 1 vs 1 (match)
- 1 vs 0 (differ)
- 1 vs 0 (differ)
- 1 vs 0 (differ)
- 1 vs 0 (differ)
- 0 vs 1 (differ)
- 0 vs 1 (differ)
- 0 vs 1 (differ)
Counting the differences, we find that the Hamming distance is 7.
What is the Hamming distance between 10101 and 11110?
For this 5-bit pair:
10101
11110
Bit-wise comparison:
- 1 vs 1 (match)
- 0 vs 1 (differ)
- 1 vs 1 (match)
- 0 vs 1 (differ)
- 1 vs 0 (differ)
The Hamming distance here is 3.
What is the Hamming distance between 10101010 and 10010010?
Examining this 8-bit pair:
10101010
10010010
Comparison:
- 1 vs 1 (match)
- 0 vs 0 (match)
- 1 vs 0 (differ)
- 0 vs 1 (differ)
- 1 vs 0 (differ)
- 0 vs 0 (match)
- 1 vs 1 (match)
- 0 vs 0 (match)
The Hamming distance in this case is 3.
Sample Calculations Using the Hamming Distance Calculator
To further illustrate the concept, let’s create a table of sample calculations:
References
- Hamming, R. W. (1950). Error detecting and error correcting codes. The Bell System Technical Journal, 29(2), 147-160. https://ieeexplore.ieee.org/document/6772729
- MacKay, D. J. (2003). Information theory, inference and learning algorithms. Cambridge University Press. https://www.cambridge.org/core/books/information-theory-inference-and-learning-algorithms/2E26154E6D44BDCC0F6C78CE2F8AEB9F
- Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. (2009). Introduction to algorithms. MIT Press. https://mitpress.mit.edu/books/introduction-algorithms-third-edition
Related Tools: