Google – Hamming Distance of Two Binary Code
求a和b的hamming distance, a和b都小于2^64.
[Solution]
hamming distance就是两个integer的binary representation有多少个bit不同。
直接xor算1的个数就好了。
如果内存小于2^64(约等于7G),那就分段来比。
但是Reference里提供的面经里有说存字典,想了好久也没想明白怎么存字典…
http://www.1point3acres.com/bbs/thread-138233-1-1.html
Read full article from Google – Hamming Distance of Two Binary Code
求a和b的hamming distance, a和b都小于2^64.
[Solution]
hamming distance就是两个integer的binary representation有多少个bit不同。
直接xor算1的个数就好了。
如果内存小于2^64(约等于7G),那就分段来比。
但是Reference里提供的面经里有说存字典,想了好久也没想明白怎么存字典…
public
int
hammingDistance(
long
a,
long
b) {
long
x = a ^ b;
int
result =
0
;
for
(
int
i =
0
; i <
64
; i++) {
result += (x &
1
);
x >>=
1
;
}
return
result;
}
http://www.1point3acres.com/bbs/thread-138233-1-1.html
Read full article from Google – Hamming Distance of Two Binary Code