BigIntegerMath ceilingPowerOfTwo() function | Guava | Java
The ceilingPowerOfTwo(BigInteger x) method of Guava’s BigIntegerMath class returns the smallest power of two greater than or equal to x. This is equivalent to BigInteger.valueOf(2).pow(log2(x, CEILING)).
public static BigInteger ceilingPowerOfTwo(BigInteger x)
Parameters: This method takes the number x as parameter whose ceiling power of two is to be found.
Return Value: This method returns the ceiling power of two of the given number x.
Exceptions: This method throws IllegalArgumentException if x <= 0.
Below examples illustrates the BigIntegerMath.ceilingPowerOfTwo() method:
Smallest power of 2 greater than or equal to 25 is: 32 Smallest power of 2 greater than or equal to 65 is: 128
Exception: java.lang.IllegalArgumentException: x (0) must be > 0
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.