& 相同为1,不同为0,注意判断条件不能用>0,因为java存在负号的可能性,无符号位移用>>>
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int res = 0;
while(n!=0){
if((n&1) == 1) res++;
n>>>=1;
}
return res;
}
}