I am looking for a fast way to approximate the logarithm base 2 of a BigInteger
. Speed and support for large numbers are more important than exact results. My current suggestion is:
return BigInteger.valueOf(n.bitLength());
I highly assume that this gives a correct approximation for numbers up to 2^(2^31-1). But can it be done faster? I am not sure how efficient bitLength()
is?
All comments and suggestions are highly appreciated. Thanks in advance.
Update: This is not a duplicate, because I was looking for the fastest way to perform the operation where accuracy is not that much of an issue. In the end, I decided to use double
approximations.