How to compile database 2 using bitwise operators?

I need to compute a database of 2 numbers in C, but I cannot use the math library. The answer does not have to be accurate, just closer to int int. I thought about this, and I know that I can just use the while loop and continue dividing the number by 2 until it becomes <2 and save the number of iterations, but is this possible with bitwise operators?

+5
source share
2 answers

If you consider shifting as a bitwise operator, this is easy.

You already know how to do this by dividing by 2 sequentially.

x >> 1matches with x / 2for any unsigned integer in C.

, " " -, , 4 , 0, 4 . 16 19 63. , . , 16, 4, 1. , , 1024- , .

+6

abamert, , :

Log2(x) = result
while (x >>= 1) result++;   
+10

All Articles