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?
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.
x >> 1
x / 2
, " " -, , 4 , 0, 4 . 16 19 63. , . , 16, 4, 1. , , 1024- , .
abamert, , :
Log2(x) = result while (x >>= 1) result++;