Problem
Calculate how many 1's in an integer
Solution
#include <iostream>
using namespace std;
int bit_count(int x)
{
int cnt = 0;
while(x)
{
cnt ++;
x = x&(x-1);
}
return cnt;
}
int main(int argc, char* argv[])
{
for(int i = 0; i < 100; i++){
cout << hex << i << " -- " << bit_count(i) << endl;
}
return 0;
}