输入一个无符号整数x,输出x的二进制表示中1的个数.
输入:
76584
输出:
7
这里介绍了用位运算来求解
import java.util.Scanner;
public class Main {
/**
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int s = 0;
for (int i = 0; i <32; i++) {
if (((a&(1<<i))==(1<<i))) {
s++;
}
}
System.out.println(s);
}
}
输入:
76584
输出:
7