把一个 32-bit 整型转成二进制,其中包含多少个 1 ,比如 5 的二进制表达是 101 ,其中包含 2 个 1
数据范围:输入的数字满足 
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int digit = Integer.valueOf(sc.nextLine());
int num = getNumOfOne(digit);
System.out.println(num);
}
public static int getNumOfOne(int digit) {
int num = 0;
for (int i = 0; i < 32; i++) {
//例:10011011
// 每次右移一位,判断是否为1
num += digit & 1;
digit >>>= 1;
}
return num;
}
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
/*
* 将输入的数num与1做&运算,这样会取到num二进制的
* 的最后一位(0或1),之后将num右移一位,与1做&运算后会取到num的第二位
* 依次类推就能得出num每一位上的1
*
* eg:
* 101 & 001 = 001 count == 0 + 1
* 101 >>> 1 = 010
* 010 & 001 = 000 count == 1 + 0
* 010 >>> 1 = 001
* 001 & 001 = 001 count == 1 + 1
*
*/
int count = 0;
while (num > 0) {
count += num & 1;
num = num >>> 1;
}
System.out.print(count);
}
} import java.util.Scanner;
/**
* @Author: coderjjp
* @Date: 2020-05-10 9:45
* @Description: 二进制中有多少个1
* @version: 1.0
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int ans = 0;
while (n > 0){
if ((n & 1) > 0)
ans++;
n >>>= 1;
}
System.out.println(ans);
}
} import java.util.Scanner;
import java.io.*;
public class Main{
public static void main(String[] args) throws IOException{
Scanner sc = new Scanner(System.in);
String str = Integer.toBinaryString(sc.nextInt());
int count = 0;
for(int i = 0;i < str.length();i++){
if(str.charAt(i) == '1'){
count++;
}
}
System.out.println(count);
}
} import java.util.*;
public class Main
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
while(sc.hasNextInt())
{
int a=sc.nextInt();
String str=Integer.toBinaryString(a);
int count=0;
for(int i=0;i<str.length();i++)
{
if((""+str.charAt(i)).equals("1"))
{
count++;
}
}
System.out.println(count);
}
}
}