# 1、位运算
# AcWing 801. 二进制中1的个数
# n>>k&1 //取出二进制第k位的数字
import java.util.*;
//ACWing
public class Main {
public static void main(String[] args) {
Main main = new Main();
main.erjingzhi1();
}
//求的是最后一位1
//例如10010
//补码=~10010+1=01110
//两者做与运算得
//00010
int lowbit(int n){
return n&-n;
}
//判断子序列
void erjingzhi1() {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
int a[]=new int[n];
for (int i = 0; i < n; i++) a[i]=scanner.nextInt();
for (int i = 0; i < n; i++) {
int res=0;
while(a[i]>0){
a[i]-=lowbit(a[i]);
res++;
}
System.out.print(res+" ");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
← 算法基础 1、前缀和&二维前缀和 →