求一个整数,在内存当中存储时,二进制1的个数。

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
int count = 0;
for(int i = 0;i < 32;i++) {
    if(((num >> i) & 1) == 1) {
        count++;
    }
}
System.out.println(count);

}
}

posted @ 2021-01-06 18:58  LinYanyan  阅读(73)  评论(0编辑  收藏  举报