//Method 1 int count = __builtin_popcount(number); //Method 2 int count = 0; while (number) { count += number & 1; n >>= 1; }