public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int weight = 0;
while (n != 0) {
if ((n & 1) == 1) {//负数求余数可能得-1,所以得写成 if((n%2)==1||(n%2)==-1)
weight++;
}
n = n >>> 1;//注意>>> >>的区别
}
return weight;
}
}