BigInteger bitLength() in Java

bitLength(): This method is available in java.math.BigInteger class of Java.

Syntax:

int java.math.BigInteger.bitLength()

This 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.

Returns: number of bits in the minimal two's-complement representation of this BigInteger, excluding a sign bit.

For Example:

BigInteger biginteger = new BigInteger("5")

bigInteger.bitLength() = > It returns 5 ( 101).

Approach

Java

import java.math.BigInteger;

public class BigIntegerbitLength {
    public static void main(String[] args) {

        BigInteger biginteger = new BigInteger("5");
        System.out.println(biginteger.bitLength());
    }
}

Output:

3

No comments:

Post a Comment