算法1
题意:计算二进制数中1的个数
思路:lowbit()常用函数
C++ 代码
class Solution {
public:
uint32_t lowbit(uint32_t x)
{
return x &-x; //返回末尾1
}
int hammingWeight(uint32_t n) {
int cnt=0;
while(n)n-=lowbit( n),cnt++;
return cnt;
}
};