146 整数的二进制数表达中有多少个1
ac
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static Scanner sc = new Scanner(System.in);
public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static long mod = (long)Math.pow(2,29);
public static void main(String[] args) throws Exception {
int n = sc.nextInt();
System.out.println(getOneCnt(n));
}
static int getOneCnt(int n){
int cnt = 0;
for(int i=31;i>=0;i--){
cnt += (n >> i) & 1;
}
return cnt;
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static Scanner sc = new Scanner(System.in);
public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static long mod = (long)Math.pow(2,29);
public static void main(String[] args) throws Exception {
int n = sc.nextInt();
System.out.println(getOneCnt(n));
}
static int getOneCnt(int n){
int cnt = 0;
while (n != 0){
n = n & (n-1); // 将最右边的1干掉
cnt ++;
}
return cnt;
}
}
/*
7 0111
& 0110
0110
& 0101
0100
& 0011
0000
*/
public class Main {
public static Scanner sc = new Scanner(System.in);
public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static long mod = (long)Math.pow(2,29);
public static void main(String[] args) throws Exception {
int n = Integer.MIN_VALUE;
System.out.print(n + " ");
printBit(n);
n = n - 1;
System.out.print(n + " ");
printBit(n);
}
static void printBit(int n){
for(int i=31;i>=0;i--){
int tmp = (n >> i) & 1;
System.out.print(tmp);
}
System.out.println();
}
}
/*
-2147483648 10000000000000000000000000000000
2147483647 01111111111111111111111111111111
*/