java.math.BigInteger.bitLength()方法返回此BigInteger的最小2补码表示形式中的位数,但不包括符号位。对于正数BigInteger,这等效于普通二进制表示形式的位数。 bitLength方法计算(ceil(log2(this
用法:
public int bitLength()
参数:该方法不会返回任何参数。
返回值:该方法用于返回此BigInteger的最小2补码表示形式中的位数,不包括符号位。
例子:
Input: value = 2300
Output: 12
Explanation:
Binary signed 2's complement of 2300 = 0000100011111100
first four bits are singed bit so exclude them then remaining
no of bits = 12. So bitLength in 0000100011111100 = 12.
Input: value = 5482549
Output: 23
以下示例程序旨在说明BigInteger的bitLength()方法的使用。
// Program to demonstrate bitLength() method of BigInteger
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Create BigInteger objects
BigInteger biginteger = new BigInteger("2300");
// Call bitLength() method on bigInteger
int count = biginteger.bitLength();
String result = "bitLength of " + biginteger +
" is " + count;
// Print result
System.out.println(result);
}
}
输出:
bitLength of 2300 is 12