Java Guava | ceilingPowerOfTwo() method of IntMath Class

The ceilingPowerOfTwo(int x) method of Guava’s IntMath class accepts a parameter and calculates the smallest power of two greater than the values passed in the parameter. This method is equivalent to checkedPow(2, log2(x, CEILING)).

Syntax :

public static int ceilingPowerOfTwo(int x)

Parameter: This method accepts a single parameter x which is of integer type and returns the smallest power of two greater than the values passed in the parameter.



Return Value : Smallest power of two greater than or equal to x.

Exceptions :

  • IllegalArgumentException : This method throws an IllegalArgumentException if x <= 0.
  • ArithmeticException: This method throws an ArithmeticException if the next-higher power of two is not representable as an int, i.e. when x > 2^30.

Below examples illustrate the ceilingPowerOfTwo() method of IntMath class:

Example 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show implementation of 
// isPrime(int n) 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 a1 = 63;
           
        // Using isPrime(int n) 
        // method of Guava's IntMath class
        if(IntMath.isPrime(a1))
        System.out.println(a1 + " is a prime number");
        else
        System.out.println(a1 + " is not a prime number");
           
        int a2 = 17;
           
        // Using isPrime(int n) 
        // method of Guava's IntMath class
        if(IntMath.isPrime(a2))
        System.out.println(a2 + " is a prime number");
        else
        System.out.println(a2 + " is not a prime number");
    

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show implementation of 
// ceilingPowerOfTwo(int x) method of Guava's
// IntMath class
import java.math.RoundingMode; 
import com.google.common.math.IntMath; 
  
class GFG { 
      
    static int findCeilPow(int x) 
    
        try
              
            // Using ceilingPowerOfTwo(int x) 
            // method of Guava's IntMath class 
            // This should throw "IllegalArgumentException" 
            // as x <= 0
            int ans = IntMath.ceilingPowerOfTwo(x); 
    
            // Return the answer 
            return ans; 
        
        catch (Exception e) { 
            System.out.println(e); 
            return -1
        
    
  
    // Driver code 
    public static void main(String args[]) 
    
        int n = -4;
          
        try
            // Function calling
            findCeilPow(n);; 
        
        catch (Exception e) { 
            System.out.println(e); 
        
    

chevron_right


Output :

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

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




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.