BigInteger bitLength() Method in Java
The java.math.BigInteger.bitLength() method 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. The bitLength method Computes (ceil(log2(this < 0 ? -this : this+1))).
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
public int bitLength()
Parameters: The method deoes not return any parameters.
Return Value: The method is used to return the number of bits in the minimal two’s-complement representation of this BigInteger, excluding a sign bit.
Input: value = 2300 Output: 12 Explanation: Binary signed 2's complement of 2300 = 0000100011111100 first four bits are singed bit so exclude them then remaining no of bits = 12. So bitLength in 0000100011111100 = 12. Input: value = 5482549 Output: 23
Below program illustrates the use of bitLength() method of BigInteger.
bitLength of 2300 is 12