Open In App

BigInteger bitLength() Method in Java

Last Updated : 24 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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))).
Syntax: 

public int bitLength()


Parameters: The method does not take 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.
Examples: 
 

Input: value = 2300
Output: 12
Explanation:
Binary signed 2’s complement of 2300 = 0000100011111100
first four bits are signed bit so exclude them than remaining
no of bits = 12. So bitLength in 0000100011111100 = 12.

Input: value = 5482549
Output: 23


The below program illustrates the use of the bitLength() method of BigInteger. 
 

Java
// Program to demonstrate bitLength() method of BigInteger 

import java.math.*;

public class GFG {

    public static void main(String[] args)
    {

        // Create  BigInteger objects
        BigInteger biginteger = new BigInteger(&quot;2300&quot;);

        // Call bitLength() method on bigInteger
        int count = biginteger.bitLength();

        String result = &quot;bitLength of  &quot; + biginteger +
        &quot; is &quot; + count;

        // Print result
        System.out.println(result);
    }
}

Output
bitLength of  2300 is 12


 



Next Article

Similar Reads