public class Solution {
public int NumberOf1(int n) {
int num = 0;
// 负数在计算机中以补码形式存在,补码为原码最高位不变,其余位取反再加1
// ~取反操作符,是连最高位也取反。
// 整数左移在末尾添0,右移在前面添0
// 负数左移在末尾添0,右移在前面添1
while (n != 0) {
num += (n & 1);
n = n >>> 1;
}
return num;
}
}