Reputation: 1636
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
Reputation: 4951
Use bitLength()
(source)
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.
BigInteger bi;
bi = new BigInteger("778674");
System.out.println("length: " + bi.bitLength());
Upvotes: 5