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