In information theory, the Hamming distance between two strings of equal length is the number of positions at which the corresponding are different. In other words, it measures the minimum number of substitutions required to change one string into the other, or 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 for measuring the edit distance between two sequences.
A major application is in coding theory, more specifically to , in which the equallength strings are Vector space over a finite field.
For binary strings a and b the Hamming distance is equal to the number of ones (Hamming weight) in a Exclusive or b. The metric space of length n binary strings, with the Hamming distance, is known as the Hamming cube; it is equivalent as a metric space to the set of distances between vertices in a hypercube graph. One can also view a binary string of length n as a vector in $\backslash mathbb\{R\}^\{n\}$ by treating each symbol in the string as a real coordinate; with this embedding, the strings form the vertices of an ndimensional hypercube, and the Hamming distance of the strings is equivalent to the Manhattan distance between the vertices.
A code C is said to be kerrors 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 less than k. In other words, a code is kerrors correcting if, and only if, the minimum Hamming distance between any two of its codewords is at least 2 k+1. This is more easily understood geometrically as any closed balls of radius k centered on distinct codewords being disjoint. These balls are also called in this context.
Thus a code with minimum Hamming distance d between its codewords can detect at most d1 errors and can correct ⌊( d1)/2⌋ errors. The latter number is also called the packing radius or the errorcorrecting capability of the code.
It is used in telecommunication to count the number of flipped bits in a fixedlength binary word as an estimate of error, and therefore is sometimes called the signal distance.
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 like the Levenshtein distance is more appropriate.
"""Return the Hamming distance between equallength sequences""" if len(s1) != len(s2): raise ValueError("Undefined for sequences of unequal length") return sum(el1 != el2 for el1, el2 in zip(s1, s2))where the zip() function merges two equallength collections in pairs.
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 lowestorder nonzero bit. Some compilers support the __builtin_popcount function which can calculate this using specialized processor hardware where available.
int dist = 0; unsigned val = x ^ y;
// Count the number of bits set while (val != 0) { // A bit is set, so increment the count and clear the bit dist++; val &= val  1; }
// Return the number of differing bits return dist;}
Or, a much faster hardware alternative (for compilers that support builtins) is to use popcount like so.
return __builtin_popcount(x ^ y);} //if your compiler supports 64bit integers int hamming_distance(unsigned long long x, unsigned long long y) {
return __builtin_popcountll(x ^ y);}

