题目
链接
题解
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int res = 0;
for (int i = 0; i < 32; i++) {
// 将1分别移动到对应的byte上和其与操作如果不等于0该位一定为1
if ((n & (1 << i)) != 0) {
res += 1;
}
}
return res;
}
}