Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Hamming distance
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== 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> Or, in a shorter expression: <syntaxhighlight lang="python"> sum(char1 != char2 for char1, char2 in zip(string1, string2, strict=True)) </syntaxhighlight> The function <code>hamming_distance()</code>, implemented in [[Python (programming language)|Python 3]], computes the Hamming distance between two strings (or other [[Iterator|iterable]] objects) of equal length by creating a sequence of Boolean values indicating mismatches and matches between corresponding positions in the two inputs, then summing the sequence with True and False values, interpreted as one and zero, respectively. {{Clear}} <syntaxhighlight lang="python"> def hamming_distance(s1: str, s2: str) -> int: """Return the Hamming distance between equal-length sequences.""" if len(s1) != len(s2): raise ValueError("Undefined for sequences of unequal length.") return sum(char1 != char2 for char1, char2 in zip(s1, s2)) </syntaxhighlight> where the [https://docs.python.org/3/library/functions.html#zip zip()] function merges two equal-length collections in pairs. The following [[C (programming language)|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 operation|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 {{harvtxt|Wegner|1960}} that repeatedly finds and clears the lowest-order nonzero bit. Some compilers support the [[Hamming weight#Language support|__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>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Hamming distance
(section)
Add topic