And here is cool use of that:
Suppose we want to do a binary search and we have 2047 elements. We would like to know the next power of 2 which is 2048. We take the ln(2047)/ln(2) and get 10.998xxx.
If we up that to the nearest Int(10.98)+0.5) we get 11
2^11 is 2048
Now we can split at 1024 and do the binary search.
That is cool, But you can round up to nearest power of two (using unsigned integers) with (C code):
x = (x+7) & -8;
Edit: My mistake.. the above code rounds up to the next multiple of 8. Not the next power of 2. I leave it as it is.
Last edited: