Here you can find the source of getSize(BigInteger number)
number
is less than zero.
Parameter | Description |
---|---|
number | a number |
public static int getSize(BigInteger number)
//package com.java2s; import java.math.BigInteger; public class Main { /**//from ww w . j a va2s . com * Returns the number of bits in the two's-complement representation of the * given number, including a sign bit <i>only</i> if <code>number</code> is * less than zero. * * @param number * a number * @return the number of bits in the two's-complement representation of the * given number, <i>including</i> a sign bit */ public static int getSize(BigInteger number) { int cmp = number.compareTo(BigInteger.ZERO); if (cmp == 0) { // 0 is represented as a uint(size=1) return 1; } else { int bitLength = number.bitLength(); return (cmp > 0) ? bitLength : bitLength + 1; } } /** * Returns the number of bits in the two's-complement representation of the * given number, including a sign bit <i>only</i> if <code>number</code> is * less than zero. * * @param number * a number * @return the number of bits in the two's-complement representation of the * given number, <i>including</i> a sign bit */ public static int getSize(long number) { return getSize(BigInteger.valueOf(number)); } }