文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. Description
2. Solution
- Version 1
class Solution {
public:
int hammingWeight(uint32_t n) {
int count = 0;
int m = 1;
while(n) {
count += (m & n);
n >>= 1;
}
return count;
}
};
- Version 2
class Solution {
public:
int hammingWeight(uint32_t n) {
int count = 0;
int m = 1;
while(n) {
count++;
n &= (n - 1);
}
return count;
}
};