Guava BigIntegerMath类的floorPowerOfTwo(BigInteger x)方法返回小于或等于x的2的最大幂。这等效于BigInteger.valueOf(2).pow(log2(x,FLOOR))。
用法:
public static BigInteger floorPowerOfTwo(BigInteger x)
参数:此方法将BigInteger x作为其底方幂为2的参数。
返回值:此方法返回小于或等于x的2的最大幂。
异常:如果x <= 0,则此方法引发IllegalArgumentException。
以下示例说明了BifIntegerMath.floorPowerOfTwo()方法:
范例1:
// Java code to show implementation of
// floorPowerOfTwo() 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 n1 = BigInteger.valueOf(10);
// Using floorPowerOfTwo(BigInteger x) method
// of Guava's BigIntegerMath class
BigInteger
ans1
= BigIntegerMath.floorPowerOfTwo(n1);
System.out.println("Largest power of 2 less "
+ "than or equal to " + n1
+ " is:" + ans1);
BigInteger n2 = BigInteger.valueOf(127);
// Using floorPowerOfTwo(BigInteger x) method
// of Guava's BigIntegerMath class
BigInteger
ans2
= BigIntegerMath.floorPowerOfTwo(n2);
System.out.println("Largest power of 2 less "
+ "than or equal to " + n2
+ " is:" + ans2);
}
}
输出:
Largest power of 2 less than or equal to 10 is:8 Largest power of 2 less than or equal to 127 is:64
范例2:显示IllegalArgumentException
// Java code to show implementation of
// floorPowerOfTwo(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[])
{
try {
BigInteger n1 = BigInteger.valueOf(-3);
// Using floorPowerOfTwo(BigInteger x) method
// of Guava's BigIntegerMath class
// This should raise "IllegalArgumentException"
// as x <= 0
BigInteger
ans1
= BigIntegerMath.floorPowerOfTwo(n1);
System.out.println("Largest power of 2 less "
+ "than or equal to " + n1
+ " is:" + ans1);
}
catch (Exception e) {
System.out.println("Exception:" + e);
}
}
}
输出:
Exception:java.lang.IllegalArgumentException:x (-3) must be > 0
相关用法
- Java Guava BigIntegerMath log2()用法及代码示例
- Java Guava BigIntegerMath isPowerOfTwo()用法及代码示例
- Java Guava BigIntegerMath factorial()用法及代码示例
- Java Guava BigIntegerMath divide()用法及代码示例
- Java Guava BigIntegerMath ceilingPowerOfTwo()用法及代码示例
- Java Guava BigIntegerMath binomial()用法及代码示例
- Java Guava BigIntegerMath sqrt()用法及代码示例
- Java Guava BigIntegerMath log10()用法及代码示例
- Java Guava IntMath floorPowerOfTwo()用法及代码示例
- Java Guava Ints max()用法及代码示例
- Java Guava Ints contains()用法及代码示例
- Java Guava Ints min()用法及代码示例
- Java Guava Sets union()用法及代码示例
- Java Guava Ints concat()用法及代码示例
注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 BigIntegerMath floorPowerOfTwo() function | Guava | Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。