I would like to be able to tell how big in bits is a variable of type BigInteger in my program.
我希望能够在程序中告诉BigInteger类型的变量有多大。
Is this possible?
这可能吗?
1 个解决方案
#1
Use bitLength()
(source)
使用bitLength()(源码)
The
java.math.BigInteger.bitLength()
returns the number of bits in the minimal two's-complement representation of thisBigInteger
, excluding a sign bit. For positiveBigIntegers
, this is equivalent to the number of bits in the ordinary binary representation.java.math.BigInteger.bitLength()返回此BigInteger的最小二进制补码表示中的位数,不包括符号位。对于正BigIntegers,这相当于普通二进制表示中的位数。
BigInteger bi;
bi = new BigInteger("778674");
System.out.println("length: " + bi.bitLength());
#1
Use bitLength()
(source)
使用bitLength()(源码)
The
java.math.BigInteger.bitLength()
returns the number of bits in the minimal two's-complement representation of thisBigInteger
, excluding a sign bit. For positiveBigIntegers
, this is equivalent to the number of bits in the ordinary binary representation.java.math.BigInteger.bitLength()返回此BigInteger的最小二进制补码表示中的位数,不包括符号位。对于正BigIntegers,这相当于普通二进制表示中的位数。
BigInteger bi;
bi = new BigInteger("778674");
System.out.println("length: " + bi.bitLength());