Java Guava | isPowerOfTwo() method IntMath Class

The isPowerOfTwo() method of Guava’s IntMath class is used to check if a number is power of two or not. It accepts the number to be checked as a parameter and return boolean value true or false based on whether the number is a power of 2 or not.

Syntax :

public static boolean isPowerOfTwo(int x)

Parameter: This method accepts a single parameter x which of integer type.



Return Value : The method returns true if x represents power of 2 and false if x doesn’t represent power of 2.

Exceptions : The method doesn’t have any exception.

Note : This differs from Integer.bitCount(x) == 1, because Integer.bitCount(Integer.MIN_VALUE) == 1, but Integer.MIN_VALUE is not a power of two.

Example 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show implementation of
// isPowerOfTwo(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 a1 = 63;
  
        // Using isPowerOfTwo(int x) method
        // of Guava's IntMath class
        if (IntMath.isPowerOfTwo(a1))
            System.out.println(a1 + " is power of 2");
        else
            System.out.println(a1 + " is not power of 2");
  
        int a2 = 1024;
  
        // Using isPowerOfTwo(int x) method
        // of Guava's IntMath class
        if (IntMath.isPowerOfTwo(a2))
            System.out.println(a2 + " is power of 2");
        else
            System.out.println(a2 + " is not power of 2");
    }
}

chevron_right


Output :

63 is not power of 2
1024 is power of 2

Example 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show implementation of
// isPowerOfTwo(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 a1 = 10;
  
        // Using isPowerOfTwo(int x) method
        // of Guava's IntMath class
        if (IntMath.isPowerOfTwo(a1))
            System.out.println(a1 + " is power of 2");
        else
            System.out.println(a1 + " is not power of 2");
  
        int a2 = 256;
  
        // Using isPowerOfTwo(int x) method
        // of Guava's IntMath class
        if (IntMath.isPowerOfTwo(a2))
            System.out.println(a2 + " is power of 2");
        else
            System.out.println(a2 + " is not power of 2");
    }
}

chevron_right


Output :

10 is not power of 2
256 is power of 2

Reference :
https://google.github.io/guava/releases/20.0/api/docs/com/google/common/math/IntMath.html#isPowerOfTwo-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.