Hamming distance
https://en.wikipedia.org/wiki/Hamming_distance
the Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different. In another way, 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.
Hamming distance between two strings of equal length is equal to the total number of positions at which corresponding characters in the two strings are different.
Hamming distance between two integers
https://en.wikipedia.org/wiki/Hamming_distance
the Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different. In another way, 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.
Hamming distance between two strings of equal length is equal to the total number of positions at which corresponding characters in the two strings are different.
int
hammingDistance(
char
* str1,
char
* str2)
{
// If any of the string is null
// or they are not of the same size, then Hamming distance cannot be calculated.
if
(str1 == NULL || str2 == NULL ||
strlen
(str1) !=
strlen
(str2))
return
-1;
int
len =
strlen
(str1);
int
hDistance = 0;
for
(
int
i=0; i<len; i++)
if
(str1[i] != str2[i])
hDistance++;
return
hDistance;
}
Hamming distance between two integers
int hamming_distance(unsigned x, unsigned y) { int dist; unsigned val; dist = 0; val = x ^ y; // XOR // 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; }http://www.fgdsb.com/2015/01/03/hamming-distance/
Read full article from Hamming distanceint hamming(int a , int b) {int res = 0, i, j;while (a >0 || b > 0) {i = a % 10, j = b % 10;if (a == 0 || b == 0 || i != j)res++;a /= 10, b /= 10;}return res;}