BigIntegerMath ceilingPowerOfTwo() function | Guava | Java
The ceilingPowerOfTwo(BigInteger x) method of Guava’s BigIntegerMath class returns the smallest power of two greater than or equal to x. This is equivalent to BigInteger.valueOf(2).pow(log2(x, CEILING)).
Syntax:
public static BigInteger
ceilingPowerOfTwo(BigInteger x)
Parameters: This method takes the number x as parameter whose ceiling power of two is to be found.
Return Value: This method returns the ceiling power of two of the given number x.
Exceptions: This method throws IllegalArgumentException if x <= 0.
Below examples illustrates the BifIntegerMath.ceilingPowerOfTwo() method:
Example 1:
// Java code to show implementation of // ceilingPowerOfTwo(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[]) { BigInteger n1 = BigInteger.valueOf(25); // Using ceilingPowerOfTwo(BigInteger x) method of // Guava's BigIntegerMath class BigInteger ans = BigIntegerMath.ceilingPowerOfTwo(n1); System.out.println("Smallest power of 2 greater " + "than or equal to " + n1 + " is: " + ans); BigInteger n2 = BigInteger.valueOf(65); // Using ceilingPowerOfTwo(BigInteger x) method of // Guava's BigIntegerMath class BigInteger ans1 = BigIntegerMath.ceilingPowerOfTwo(n2); System.out.println("Smallest power of 2 greater " + "than or equal to " + n2 + " is: " + ans1); } } |
Output:
Smallest power of 2 greater than or equal to 25 is: 32 Smallest power of 2 greater than or equal to 65 is: 128
Example 2:
// Java code to show implementation of // ceilingPowerOfTwo(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 n = BigInteger.valueOf(0); // Using ceilingPowerOfTwo(BigInteger x) method of // Guava's BigIntegerMath class // This should raise "IllegalArgumentException" // as n is <= 0 BigInteger ans = BigIntegerMath.ceilingPowerOfTwo(n); System.out.println("Smallest power of 2 greater " + "than or equal to n is : " + ans); } catch (Exception e) { System.out.println("Exception: " + e); } } } |
Output:
Exception: java.lang.IllegalArgumentException: x (0) must be > 0
Recommended Posts:
- BigIntegerMath log2() function | Guava | Java
- BigIntegerMath isPowerOfTwo() function | Guava | Java
- BigIntegerMath factorial() function | Guava | Java
- BigIntegerMath binomial() function | Guava | Java
- BigIntegerMath divide() function | Guava | Java
- BigIntegerMath floorPowerOfTwo() function | Guava | Java
- BigIntegerMath sqrt() function | Guava | Java
- BigIntegerMath log10() function | Guava | Java
- Java Guava | ceilingPowerOfTwo() method of IntMath Class
- BigIntegerMath Class | Guava | Java
- Ints min() function | Guava | Java
- Ints contains() function | Guava | Java
- Ints max() function | Guava | Java
- Ints indexOf() function | Guava | Java
- Ints toArray() function | Guava | Java
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.



