Calculate log base 2 of an integer in Java
This post will discuss how to calculate log base 2 of an integer value in Java.
To find the logarithm of an integer to the base 2, we need to find the power to which 2 must be raised to obtain the given integer. For example, if x = 8, the result must be 3 because 2^3 = 8.
1. Using Math.log() method
The java.lang.Math class contains static methods for calculating the logarithms to bases 10 and e. Its Math.log() method returns the natural logarithm (base e), and the Math.log10() method returns the base 10 logarithms of a double value. However, the Math class does not provide any method to calculate logarithms with respect to any base b. Mathematically, this can be determined using either of these two logarithms:

Programmatically, this can be done in Java as follows:
|
1 2 3 |
public static int log(int x, int b) { return (int) (Math.log(x) / Math.log(b)); } |
So to calculate the base 2 logarithm of an integer value we can simply use the Math.log() method to get the natural logarithm and divide the result by Math.log(2) obtain base 2 logarithm. Here is an example of this method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Main { public static int log2(int x) { return (int) (Math.log(x) / Math.log(2)); } public static void main(String[] args) { int x = 8; int y = log2(x); System.out.println(y); // 3 } } |
The advantage of using this method is that it is simple and convenient. However, it may not be accurate or reliable as it depends on the floating-point arithmetic. To completely eliminate errors, add an epsilon between 1e-11 and 1e-14, as shown below:
|
1 2 3 |
public static int log(int x) { return (int) (Math.log(x) / Math.log(2) + 1e-10); } |
2. Using Integer.numberOfLeadingZeros() method
The Integer.numberOfLeadingZeros() is another built-in method that returns the number of zero bits preceding the highest-order one bit in the binary representation of an int value. We can use this method to calculate log base 2 of an integer in Java by subtracting the result of calling it from 31 to get the position of the highest-order one bit in the binary representation of the integer value. Here is an example:
|
1 2 3 4 5 6 7 8 9 10 |
class Main { public static void main(String[] args) { int x = 8; int y = 31 - Integer.numberOfLeadingZeros(x); System.out.println(y); // 3 } } |
The advantage of using this method is that it is fast and accurate, and it does not require any casting. However, it may not be intuitive or easy to understand, as it relies on the binary representation and bit manipulation of the integer value.
3. Using a custom function
Final option is to write a custom binary logarithm function that uses a simple algorithm to calculate log base 2 of an integer in Java. The idea is to use a loop to divide the integer value by two and increment the result by one in each iteration, until the integer value is greater than one. The algorithm can be implemented as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Main { public static int log2(int x) { int result = 0; while (x > 1) { x /= 2; result++; } return result; } public static void main(String[] args) { int x = 8; int y = log2(x); System.out.println(y); // 3 } } |
The advantage of using this method is that it is easy and customizable per our needs. However, it may not be very efficient or scalable, as it uses a loop and a division operation in each iteration.
That’s all about calculating log base 2 of an integer in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)