Use POPCNT instruction in C:
int result;
unsigned long long x;
...
result = __builtin_popcountll (x);
Compile the code with -mpopcnt
option in gcc
.
Check if your cpu has popcnt instruction:
grep --color popcnt /proc/cpuinfo
Reference: Stackoverflow