📜  BigIntegerMath floorPowerOfTwo()函数|番石榴 |Java

📅  最后修改于: 2022-05-13 01:55:46.089000             🧑  作者: Mango

BigIntegerMath floorPowerOfTwo()函数|番石榴 |Java

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
下面的示例说明了 BigIntegerMath.floorPowerOfTwo() 方法:
示例 1:

Java
// 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);
    }
}


Java
// 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);
        }
    }
}


输出:

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

// 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

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