java求二进制中1的个数
将有符号数转化为无符号数,即此时不用区分正数还是负数;java中转化为无符号数可以使用包装类中定义的Integer.toUnsignedLong()
class Solution {
public int NumberOf1(int n)
{
long temp = Integer.toUnsignedLong(n);
int count = 0;
while (temp > 0) {
if ((temp & 1) == 1)
count++;
temp = temp >> 1;
}
return count;
}
}