有没有办法获得'BigInteger'变量的总大小?

时间:2022-07-19 16:47:23

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 this BigInteger, excluding a sign bit. For positive BigIntegers, 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 this BigInteger, excluding a sign bit. For positive BigIntegers, 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());