Thursday, June 11, 2026
HomeLanguagesJavaBigIntegerMath isPowerOfTwo() function | Guava | Java

BigIntegerMath isPowerOfTwo() function | Guava | Java

The method isPowerOfTwo(BigInteger x) of Guava’s BigIntegerMath class returns true if x represents a power of two. 
Syntax: 
 

public static boolean isPowerOfTwo(BigInteger x)

Parameters: This method takes the BigInteger number x as parameter which is to be checked.
Return Value: This method returns true if x is a power of two.
Below examples illustrates the BigIntegerMath.isPowerOfTwo() method:
Example 1:
 

Java




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


Output: 

63 is not power of 2
1024 is power of 2

 

Example 2:
 

Java




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


Output: 

1 is power of 2
567 is not power of 2

 

Reference: https://google.github.io/guava/releases/21.0/api/docs/com/google/common/math/BigIntegerMath.html#isPowerOfTwo-java.math.BigInteger-
 

RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS