Q:

Number of bits allocated to 0 is 0 in java- Why?

While learning BigInteger.valueOf(0L).bitLength() and BigInteger.valueOf(0L).bitCount() I found it is returning 0
However, the if consider 0 as a number then it should also needs one bit. 
From my understanding I expect 1 as return value for both methods instead of 0.
public class Test {
    public static void main(String[] args) {
        int bitLength = BigInteger.valueOf(0L).bitLength();
        int bitCount = BigInteger.valueOf(0L).bitCount();
        System.out.println("bitLength=" + bitLength);
        System.out.println("bitCount=" + bitCount);
    }
 }

Please help me understand why It is behaving like this ?

A:

int bitCount()
Returns the number of bits in the two's complement representation of this BigInteger that differ from its sign bit.

For your BigInteger.valueOf(0L) example, the sign bit is 0, and there are no 1 bits, hence 0 is returned (since there are no bits that differ from the sign bit).

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

For your BigInteger.valueOf(0L) example, the minimal two's-complement representation of this BigInteger would include just the sign bit 0, so if you exclude the sign bit from the count, as the Javadoc states, you get 0.
It is interesting to note that for the same reasons BigInteger.valueOf(-1L) would also return 0 for both of these methods. Here the sign bit is 1, and after excluding it there are no other bits.

