The method floorPowerOfTwo(BigInteger x) of Guava’s BigIntegerMath class returns the largest power of two less than or equal to x. This is equivalent to BigInteger.valueOf(2).pow(log2(x, FLOOR)).
public static BigInteger floorPowerOfTwo(BigInteger x)
Parameters: This method takes the BigInteger x as parameter whose floor power of two is to be found.
Return Value: This method returns the largest power of two less than or equal to x.
Exceptions: This method throws IllegalArgumentException if x <= 0.
Below examples illustrates the BifIntegerMath.floorPowerOfTwo() method:
Largest power of 2 less than or equal to 10 is: 8 Largest power of 2 less than or equal to 127 is: 64
Example 2: To show IllegalArgumentException
Exception: java.lang.IllegalArgumentException: x (-3) 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.