Java BigInteger bitLength()方法
java.math.BigInteger.bitLength() 方法返回该BigInteger的最小二进制表示中的比特数,不包括符号位。对于正数大整数,这相当于普通二进制表示法中的位数。bitLength方法计算 _(ceil(log2(this < 0 ? -this : this+1)))。
语法:
public int bitLength()
参数: 该方法不返回任何参数。
返回值: 该方法用于返回该大整数的最小二补表示法中的位数,不包括符号位。
示例:
输入: value = 2300
输出: 12
解释:
2300的二进制符号2的补码=0000100011111100
前四位是单数位,所以排除它们,然后剩余的
的位数=12。所以0000100011111100的比特长度=12。
输入: value = 5482549
输出: 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
**参考资料: **https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#bitLength()