1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <stdio.h> int bitcount(unsigned num); int main(void) { int x = 1337; /* a random int */ printf("%d\n", bitcount(x));; return 0; } int bitcount(unsigned num) { int bits; for (bits = 0; num != 0; num &= (num-1)) bits++; return bits; }