题目描述
输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。
分析
1.位运算
public static int NumberOf1_1(int n) {
int count = 0;
while (n != 0) {
count++;
n = n & (n - 1);
}
return count;
}
2.Integer自带的方法
public int NumberOf1(int n) {
return Integer.bitCount(n);
}
网友评论