如果x表示2的冪,則Guava的BigIntegerMath類的isPowerOfTwo(BigInteger x)方法將返回true。
用法:
public static boolean isPowerOfTwo(BigInteger x)
參數:此方法將BigInteger數字x作為要檢查的參數。
返回值:如果x為2的冪,則此方法返回true。
以下示例說明了BifIntegerMath.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
相關用法
- Java Guava BigIntegerMath log10()用法及代碼示例
- Java Guava BigIntegerMath floorPowerOfTwo()用法及代碼示例
- Java Guava BigIntegerMath factorial()用法及代碼示例
- Java Guava BigIntegerMath sqrt()用法及代碼示例
- Java Guava BigIntegerMath divide()用法及代碼示例
- Java Guava BigIntegerMath ceilingPowerOfTwo()用法及代碼示例
- Java Guava BigIntegerMath binomial()用法及代碼示例
- Java Guava BigIntegerMath log2()用法及代碼示例
- Java Guava IntMath isPowerOfTwo()用法及代碼示例
- Java Guava LongMath isPowerOfTwo(long x)用法及代碼示例
- Java Guava Ints max()用法及代碼示例
- Java Guava Ints contains()用法及代碼示例
- Java Guava Ints min()用法及代碼示例
- Java Guava Ints lastIndexOf()用法及代碼示例
注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 BigIntegerMath isPowerOfTwo() function | Guava | Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。