题解:我之前写的位运算模板
图1:
class Solution {
public:
uint32_t lowbit(uint32_t x) {
return x & -x;
}
int hammingWeight(uint32_t n) {
int cnt = 0;
while (n) {
n -= lowbit(n);
cnt ++;
}
return cnt;
}
};