Hamming Distance: Difference between revisions
Jump to navigation
Jump to search
Line 7: | Line 7: | ||
=Implementations= | =Implementations= | ||
==Hamming Distance between Two Integers in Java== | ==Hamming Distance between Two Integers in Java== | ||
<syntaxhighlight lang='java'> | |||
public static int hammingDistance(int i1, int i2) { | |||
int setBits = 0; | |||
int xor = i1 ^ i2; | |||
while(xor > 0) { | |||
setBits += xor & 1; | |||
xor >>= 1; | |||
} | |||
return setBits; | |||
} | |||
</syntaxhighlight> | |||
Also see: {{Internal|Java_Language#Integer_Bitwise_XOR_.5E|Integer Bitwise XOR <tt>^</tt> in Java}} | Also see: {{Internal|Java_Language#Integer_Bitwise_XOR_.5E|Integer Bitwise XOR <tt>^</tt> in Java}} |
Revision as of 21:17, 24 October 2021
External
Internal
Overview
The Hamming distance between two string 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 on string into the other, or the minimum number of errors that could have transformed one string into the other.
Implementations
Hamming Distance between Two Integers in Java
public static int hammingDistance(int i1, int i2) {
int setBits = 0;
int xor = i1 ^ i2;
while(xor > 0) {
setBits += xor & 1;
xor >>= 1;
}
return setBits;
}
Also see: