I need to compute the log base 2 of a number in C but I cannot use the math library. The answer doesn't need to be exact, just to the closest int. I've thought about it and I know I could just use a while loop and keep dividing the number by 2 until it is < 2, and keep count of the iterations, but is this possible using bitwise operators?
Already answered by abamert but just to be more concrete this is how you would code it:
Log2(x) = result
while (x >>= 1) result++;