Java Guava | ceilingPowerOfTwo() method of IntMath Class
The ceilingPowerOfTwo(int x) method of Guava’s IntMath class accepts a parameter and calculates the smallest power of two greater than the values passed in the parameter. This method is equivalent to checkedPow(2, log2(x, CEILING)).
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
public static int ceilingPowerOfTwo(int x)
Parameter: This method accepts a single parameter x which is of integer type and returns the smallest power of two greater than the values passed in the parameter.
Return Value : Smallest power of two greater than or equal to x.
- IllegalArgumentException : This method throws an IllegalArgumentException if x <= 0.
- ArithmeticException: This method throws an ArithmeticException if the next-higher power of two is not representable as an int, i.e. when x > 2^30.
Below examples illustrate the ceilingPowerOfTwo() method of IntMath class:
Example 1 :
Smallest power of 2 greater than or equal to 25 is : 32 Smallest power of 2 greater than or equal to 65 is : 128
Example 2 :
java.lang.IllegalArgumentException: x (-4) must be > 0