This is what I need to do:
int lg(int v)
{
int r = 0;
while (v >>= 1) // unroll for more speed...
{
r++;
}
}
I found the above solution at: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog
This works, butI need to do it without loops, control structures, or constants bigger than 0xFF (255), which has proven to be very hard for me to find. I’ve been trying to figure something out using conditionals in the form
( x ? y : z ) = (((~(!!x) + 1)) & y) | ((~(~(!!x) + 1)) & z)
but I can’t get it to work. Thanks for your time.
Without any control structure, not even the
?:operator, you can simulate your own algoprovided that, in C,
int x=-1;for instancex >>= 1n times is always!= 0x != 0returns 0 (false) or 1 (*true)