# 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
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.