C++ 代码
#include <iostream>
using namespace std;
int bitcount(int n)
{
n -= (n >> 1) & 0x55555555;
n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
n = (n + (n >> 4)) & 0x0f0f0f0f;
n += (n >> 8);
n += (n >> 16);
return n & 0x3f;
}
int main(int argc, char **argv)
{
int N;
cin >> N;
while (N--)
{
int temp;
cin >> temp;
cout << bitcount(temp);
if (N)
cout << " ";
else
cout << endl;
}
return EXIT_SUCCESS;
}