PLAN 1 位运算 求x的第k位数字 x >> k & 1
class Solution {
public:
int NumberOf1(int n) {
int res=0;
for(int i=0;i<32;i++){
if(n>>i&1==1) res++;
}
return res;
}
};
PLAN 2 lowbit()写法 lowbit(x) = x & -x
class Solution {
public:
int NumberOf1(int n) {
int res=0;
while(n){
n-=-n&n;
res++;
}
return res;
}
};