Skip to content

Instantly share code, notes, and snippets.

@XcqRomance
Created December 3, 2018 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XcqRomance/4426b970611a1b1b72e8ed6de57839e3 to your computer and use it in GitHub Desktop.
Save XcqRomance/4426b970611a1b1b72e8ed6de57839e3 to your computer and use it in GitHub Desktop.
位1的个数
// 位1的个数,
int hammingWeight(uint32_t n) {
int count = 0;
while (n) {
++count;
n = (n-1)&n;
}
return count;
}
int hammingWeight2(uint32_t n) {
int count = 0;
unsigned int flag = 1;
while (flag) {
if (flag&n) {
++count;
}
flag = flag<<1;
}
return count;
}
// 汉明距离
int hammingDistance(int x, int y) {
int n = x^y;
return hammingWeight(n);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment