In information theory, the Hamming distance between two strings or vectors of equal length is the number of positions at which the corresponding symbols are different. In other words, it measures the minimum number of substitutions required to change one string into the other, or equivalently, the minimum number of errors that could have transformed one string into the other. In a more general context, the Hamming distance is one of several string metrics for measuring the edit distance between two sequences. It is named after the American mathematician Richard Hamming.
A major application is in coding theory, more specifically to block codes, in which the equal-length strings are vectors over a finite field.
Definition
The Hamming distance between two equal-length strings of symbols is the number of positions at which the corresponding symbols are different.
Examples
The symbols may be letters, bits, or decimal digits, among other possibilities. For example, the Hamming distance between:
- "kain" and "kain" is 3.
- "krin" and "krin" is 3.
- "kin" and "kin" is 4.
- and is 4.
- 2396 and 2396 is 3.
Properties
For a fixed length n, the Hamming distance is a metric on the set of the words of length n (also known as a Hamming space), as it fulfills the conditions of non-negativity, symmetry, the Hamming distance of two words is 0 if and only if the two words are identical, and it satisfies the triangle inequality as well:
For example, consider a code consisting of two codewords "000" and "111". The Hamming distance between these two words is 3, and therefore it is k=2 error detecting. This means that if one bit is flipped or two bits are flipped, the error can be detected. If three bits are flipped, then "000" becomes "111" and the error cannot be detected.
A code C is said to be k-error correcting if, for every word w in the underlying Hamming space H, there exists at most one codeword c (from C) such that the Hamming distance between w and c is at most k. In other words, a code is k-errors correcting if the minimum Hamming distance between any two of its codewords is at least 2k+1. This is also understood geometrically as any closed balls of radius k centered on distinct codewords being disjoint.
History and applications
The Hamming distance is named after Richard Hamming, who introduced the concept in his fundamental paper on Hamming codes, Error detecting and error correcting codes, in 1950. Hamming weight analysis of bits is used in several disciplines including information theory, coding theory, and cryptography.
It is used in telecommunication to count the number of flipped bits in a fixed-length binary word as an estimate of error, and therefore is sometimes called the signal distance. For q-ary strings over an alphabet of size q ≥ 2 the Hamming distance is applied in case of the q-ary symmetric channel, while the Lee distance is used for phase-shift keying or more generally channels susceptible to synchronization errors because the Lee distance accounts for errors of ±1. If <math>q = 2</math> or <math>q = 3</math> both distances coincide because any pair of elements from <math display="inline">\mathbb{Z}/2\mathbb{Z}</math> or <math display="inline">\mathbb{Z}/3\mathbb{Z}</math> differ by 1, but the distances are different for larger <math>q</math>.
The Hamming distance is also used in systematics as a measure of genetic distance.
However, for comparing strings of different lengths, or strings where not just substitutions but also insertions or deletions have to be expected, a more sophisticated metric such as the Levenshtein distance may be more appropriate.
Algorithm example
The following function, written in Python 3, returns the Hamming distance between two strings:
<syntaxhighlight lang="python3" line="1">
def hamming_distance(string1: str, string2: str) -> int:
"""Return the Hamming distance between two strings."""
if len(string1) != len(string2):
raise ValueError("Strings must be of equal length.")
dist_counter = 0
for n in range(len(string1)):
if string1[n] != string2[n]:
dist_counter += 1
return dist_counter
</syntaxhighlight>
The following C function will compute the Hamming distance of two integers (considered as binary values, that is, as sequences of bits). The running time of this procedure is proportional to the Hamming distance rather than to the number of bits in the inputs. It computes the bitwise exclusive or of the two inputs, and then finds the Hamming weight of the result (the number of nonzero bits) using an algorithm of that repeatedly finds and clears the lowest-order nonzero bit. Some compilers support the __builtin_popcount function which can calculate this using specialized processor hardware where available.
<syntaxhighlight lang="c">
int hamming_distance(unsigned x, unsigned y)
{
int dist = 0;
// The ^ operators sets to 1 only the bits that are different
for (unsigned val = x ^ y; val > 0; ++dist)
{
// We then count the bit set to 1 using the Peter Wegner way
val = val & (val - 1); // Set to zero val's lowest-order 1
}
// Return the number of differing bits
return dist;
}
</syntaxhighlight>
A faster alternative is to use the population count (popcount) assembly instruction. Certain compilers such as GCC and Clang make it available via an intrinsic function:
<syntaxhighlight lang="c">
// Hamming distance for 32-bit integers
int hamming_distance32(unsigned int x, unsigned int y)
{
return __builtin_popcount(x ^ y);
}
// Hamming distance for 64-bit integers
int hamming_distance64(unsigned long long x, unsigned long long y)
{
return __builtin_popcountll(x ^ y);
}
</syntaxhighlight>
See also
- Closest string
- Damerau–Levenshtein distance
- Euclidean distance
- Gap-Hamming problem
- Gray code
- Jaccard index
- Jaro–Winkler distance
- Levenshtein distance
- Mahalanobis distance
- Mannheim distance
- Sørensen similarity index
- Sparse distributed memory
- Word ladder
