Returning a log base 2 value from a function--bitwise operators?

kaytee

New member
I'm clueless on what to do for this. I'm asked to create a function that returns the floor of log base 2 of x, where x > 0. So for example, if a parameter is 16, it would return 4. (logFunc(16) = 4). I can only use !, ~, &, ^, |, +, <<, and >> as the operators in my function (= can be used as many times as needed).

All I know is that I need to do some sort of logical right shift...but that's it. I'm completely lost from there.
No conditionals or casting are allowed, either.
Loops aren't allowed either, I'm afraid...
 
Back
Top