Java Guava | floorPowerOfTwo() method IntMath Class
The floorPowerOfTwo() method of Guava’s IntMath class accepts a parameter and returns the largest power of two less than the value passed in the parameter. This is equivalent to: checkedPow(2, log2(x, FLOOR)).
public static int floorPowerOfTwo(int x)
Parameters: This method accepts a single parameter x which is an integral value.
Return Value : The method returns the largest power of two less than or equal to x.
Exceptions : The method floorPowerOfTwo(int x) throws IllegalArgumentException if x <= 0.
Example 1 :
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 :
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.