Java BigInteger Calculate getSize(BigInteger number)

Here you can find the source of getSize(BigInteger number)

Description

Returns the number of bits in the two's-complement representation of the given number, including a sign bit only if number is less than zero.

License

Open Source License

Parameter

Parameter Description
number a number

Return

the number of bits in the two's-complement representation of the given number, including a sign bit

Declaration

public static int getSize(BigInteger number) 

Method Source Code


//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));
    }
}

Related

  1. getPrivateKeyBytes(BigInteger privateKey)
  2. getQosFlowId(short tableId, BigInteger dpId, int lportTag)
  3. getRadixNumberInString(BigInteger integerNumber, int radix)
  4. getRandomBigInteger(int bits)
  5. getRandomInteger(BigInteger n, Random rand)
  6. getValueWithUnit(BigInteger value, final int places)
  7. getVersionString(BigInteger versionNumber)
  8. greater(BigInteger i1, BigInteger i2)
  9. hexEncode(BigInteger bigInteger)