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 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.
Examples:
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.
// 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("2300"); // Call bitLength() method on bigInteger int count = biginteger.bitLength(); String result = "bitLength of " + biginteger + " is " + count; // Print result System.out.println(result); } } |
bitLength of 2300 is 12
Reference:https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#bitLength()
Recommended Posts:
- BigInteger not() Method in Java
- BigInteger and() Method in Java
- BigInteger xor() Method in Java
- BigInteger mod() Method in Java
- BigInteger abs() Method in Java
- BigInteger or() method in Java
- BigInteger pow() Method in Java
- BigInteger toString() Method in Java
- BigInteger sqrt() Method in Java
- BigInteger andNot() Method in Java
- BigInteger bitCount() Method in Java
- BigInteger clearBit() Method in Java
- BigInteger flipBit() Method in Java
- BigInteger negate() Method in Java
- BigInteger getLowestSetBit() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



