# BigIntegerMath floorPowerOfTwo() function | Guava | Java

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

Syntax:

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

Example 1:

 `// Java code to show implementation of ` `// floorPowerOfTwo() method ` `// of Guava's BigIntegerMath class ` ` `  `import` `java.math.*; ` `import` `com.google.common.math.BigIntegerMath; ` ` `  `class` `GFG { ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``BigInteger n1 = BigInteger.valueOf(``10``); ` ` `  `        ``// Using floorPowerOfTwo(BigInteger x) method ` `        ``// of Guava's BigIntegerMath class ` `        ``BigInteger ` `            ``ans1 ` `            ``= BigIntegerMath.floorPowerOfTwo(n1); ` ` `  `        ``System.out.println(``"Largest power of 2 less "` `                           ``+ ``"than or equal to "` `+ n1 ` `                           ``+ ``" is: "` `+ ans1); ` ` `  `        ``BigInteger n2 = BigInteger.valueOf(``127``); ` ` `  `        ``// Using floorPowerOfTwo(BigInteger x) method ` `        ``// of Guava's BigIntegerMath class ` `        ``BigInteger ` `            ``ans2 ` `            ``= BigIntegerMath.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: To show IllegalArgumentException

 `// Java code to show implementation of ` `// floorPowerOfTwo(BigInteger x) method ` `// of Guava's BigIntegerMath class ` ` `  `import` `java.math.*; ` `import` `com.google.common.math.BigIntegerMath; ` ` `  `class` `GFG { ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` ` `  `        ``try` `{ ` `            ``BigInteger n1 = BigInteger.valueOf(-``3``); ` ` `  `            ``// Using floorPowerOfTwo(BigInteger x) method ` `            ``// of Guava's BigIntegerMath class ` `            ``// This should raise "IllegalArgumentException" ` `            ``// as x <= 0 ` `            ``BigInteger ` `                ``ans1 ` `                ``= BigIntegerMath.floorPowerOfTwo(n1); ` ` `  `            ``System.out.println(``"Largest power of 2 less "` `                               ``+ ``"than or equal to "` `+ n1 ` `                               ``+ ``" is: "` `+ ans1); ` `        ``} ` `        ``catch` `(Exception e) { ` `            ``System.out.println(``"Exception: "` `+ e); ` `        ``} ` `    ``} ` `} `

Output:

```Exception: 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.