public class Test4 {
public static void main(String[] args) {
check1();
}
private static void check1() {
// 统计整数的二进制表达中 有多少个1
int n = 7;
int res = 0;
while (n != 0) {
n = n & (n - 1); // 每次干掉 最右边的1
res++;
}
System.out.println(res);
}
private static void check() {
int n = 7;// 3
int num = 0;
while (n != 0) {
// num++; 等价于下方的代码
num += n & 1;
n = n >>> 1;
}
System.out.println(num);
}
}
20、统计整数的二进制表达中 有多少个1
最新推荐文章于 2024-07-29 14:22:26 发布