Hamming Distance Between Bit Strings
Hamming distance counts how many corresponding positions differ between words of equal length.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace two words
Words 1011 and 1000 differ in their last two positions, giving distance two. XOR produces a one at each difference. See bitwise XOR.
Check the input contract
Comparing words of different lengths requires a rule outside ordinary Hamming distance. For insertions and deletions, edit distance models a different set of changes.[1][2]
Mark each mismatched position before counting
Compare 101101 and 100001 at equal width six. They differ in the third and fourth positions from the left, so Hamming distance is two. Bitwise XOR forms 001100, whose population count is two. See XOR and differences.
First 101101
Second 100001
XOR 001100
Hamming distance 2
Maintain a common alignment and width
Hamming distance compares equal-length sequences at the same positions. Inserting a bit can shift many comparisons and is better modeled by an edit or framing problem when positions are lost.