Let's not consider 0
or negative values for the sake of simplicity.
Known solution #1
return (int)(Math.log(x)/Math.log(2)); // pseudo code
Problem: Numerically unstable. For x=4
input it may answer 2 or 1.
Known solution #2
for(int i=0; ;++i)
{
x = x >> 1;
if(x==0)
return i;
}
Problem: Average complexity is O(n) where n is the number of bits in type. I would like to have constant time answer.