Java BigIntegerMath isPowerOfTwo() 函数
Guava的BigIntegerMath类的方法 isPowerOfTwo(BigInteger x )如果x代表2的幂,则返回true。
语法:
public static boolean isPowerOfTwo(BigInteger x)
参数: 该方法接收要检查的大整数 x 作为参数。
返回值: 如果x是2的幂,该方法返回真。
下面的例子说明了BigIntegerMath.isPowerOfTwo()方法:
例1:
// 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");
}
}
输出
63 is not power of 2
1024 is power of 2
例2:
// 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");
}
}
输出
1 is power of 2
567 is not power of 2
参考资料: https://google.github.io/guava/releases/21.0/api/docs/com/google/common/math/BigIntegerMath.html#isPowerOfTwo-java.math.BigInteger-