Yet Another Way to count the bits is to use a language for which that is a goal. intercal is such a language (although "goal" might be a bit strong here). With the select operator, which looks like a ~, this operation becomes simple.
The way the operator works is to extract from the first operand just the bits that correspond to high bits in the second operand.

So, if we do this ("#" signifies a constant):
#185~#237

in binary we have this:
10111001 ~ 11101101

So, the second operand will select bits 8,7,6,4,3 and 1 (the 1s). In the first operand those bits are 101101, or 45.

This means that if you select a number against itself, you end up with a result composed of just the ones.

#185~#185
This is 31, or in 11111 in binary.

If our result is N, then log2(N+1) tells us the number of 1s. In this case, log2(31+1) = log2(32) = 5.
Of course, solving for can be N pretty darn difficult. factoring large numbers isn't easy..yet...