Thursday, July 23, 2026
HomeLanguagesJavaJava Guava | floorPowerOfTwo() method IntMath Class

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)).

Syntax :

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 :




// Java code to show implementation
// of floorPowerOfTwo(int x) method
// of Guava's IntMath class
import java.math.RoundingMode;
import com.google.common.math.IntMath;
  
class GFG {
  
    // Driver code
    public static void main(String args[])
    {
        int n1 = 10;
  
        // Using floorPowerOfTwo(int x) method
        // of Guava's IntMath class
        int ans1 = IntMath.floorPowerOfTwo(n1);
  
        System.out.println("Largest power of 2 less "
                           + "than or equal to " + n1 + " is : " + ans1);
  
        int n2 = 127;
  
        // Using floorPowerOfTwo(int x) method
        // of Guava's IntMath class
        int ans2 = IntMath.floorPowerOfTwo(n2);
  
        System.out.println("Largest power of 2 less "
                           + "than or equal to " + n2 + " is : " + ans2);
    }
}


Output :

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 code to show implementation
// of floorPowerOfTwo(int x) method
// of Guava's IntMath class
import java.math.RoundingMode;
import com.google.common.math.IntMath;
  
class GFG {
  
    static int findFloorPow(int x)
    {
        try {
            // Using floorPowerOfTwo(int x)
            // method of Guava's IntMath class
            // This should raise "IllegalArgumentException"
            // as x <= 0
            int ans = IntMath.floorPowerOfTwo(x);
  
            // Return the answer
            return ans;
        }
        catch (Exception e) {
            System.out.println(e);
            return -1;
        }
    }
  
    // Driver code
    public static void main(String args[])
    {
        int x = -3;
  
        try {
            // Function calling
            findFloorPow(x);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}


Output :

java.lang.IllegalArgumentException: x (-3) must be > 0

Reference :
https://google.github.io/guava/releases/20.0/api/docs/com/google/common/math/IntMath.html#floorPowerOfTwo-int-

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6902 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12114 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6980 POSTS0 COMMENTS
Umr Jansen
6971 POSTS0 COMMENTS