ealione
ealione

Reputation: 1636

Is there a way to get the total size of a 'BigInteger' variable?

I would like to be able to tell how big in bits is a variable of type BigInteger in my program.

Is this possible?

Upvotes: 0

Views: 211

Answers (1)

ByteHamster
ByteHamster

Reputation: 4951

Use bitLength() (source)

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.

BigInteger bi;
bi = new BigInteger("778674");
System.out.println("length: " + bi.bitLength());

Upvotes: 5

Related Questions