lowbit(x) :是求二进制中最低位1对应的值。
lowbit(x) = x&(~x+1)或x&(-x)
5二进制序列为 101。
lowbit(5)= 1
用lowbit求二进制中1的个数
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int sum = 0;
while (n > 0) {
n -= lowbit(n);
sum ++;
}
System.out.println(sum);
}
public static int lowbit(int n){
return n & (-n);
}
}