位运算
x & -x = x & (~x + 1)
x = 101...100...0
~x = 010...01....1
~x+1 = 010...10....0
x&(~x+1) = 0....010....0
C++ 代码
#include<iostream>
using namespace std;
int main(){
int n, x;
scanf("%d", &n);
int res;
while(n--){
res = 0;
scanf("%d", &x);
for(int i = x; i; i -= i & -i) res++;
cout << res << ' ';
}
return 0;
}