Java Utililty Methods BigInteger Calculate

List of utility methods to do BigInteger Calculate

Description

The list of methods to do BigInteger Calculate are organized into topic(s).

Method

Stringint2zpBin(BigInteger num, int len)
intzp Bin
final String zeroPadding = rep('0', len);
final String numString = num.toString(2);
return (zeroPadding.substring(numString.length()) + numString);
StringintegerToString(BigInteger value)
integer To String
StringBuilder s = new StringBuilder();
while (value.bitLength() > 0) {
    BigInteger[] result = value.divideAndRemainder(BigInteger.valueOf(62));
    value = result[0];
    char c = intToChar(result[1].intValue());
    s.insert(0, c);
return s.toString();
...
intintValue(BigInteger bi)
Converts a BigInteger to an integer, if the BigInteger is in the correct range.
if (bi.compareTo(MIN_INT_BIG_INTEGER) < 0)
    throw new IllegalArgumentException("BigInteger [" + bi + "] is too small to convert to int.");
if (bi.compareTo(MAX_INT_BIG_INTEGER) > 0)
    throw new IllegalArgumentException("BigInteger [" + bi + "] is too big to convert to int.");
return bi.intValue();
intintValueExact(BigInteger bigint)
int Value Exact
if (bigint == null || bigint.bitLength() > 31) {
    throw new ArithmeticException();
return bigint.intValue();
BigIntegerjsonBigInteger(JsonValue value)
json Big Integer
if (value == null || value.getValueType() == JsonValue.ValueType.NULL)
    return null;
if (value.getValueType() == JsonValue.ValueType.NUMBER && (value instanceof JsonNumber))
    return ((JsonNumber) value).bigIntegerValue();
if (value.getValueType() == JsonValue.ValueType.STRING && (value instanceof JsonString)) {
    try {
        return new BigInteger(((JsonString) value).getString());
    } catch (NumberFormatException ex) {
...
intlength(BigInteger bi)
length
int length = 0;
while (bi.compareTo(BigInteger.ZERO) > 0) {
    bi = bi.divide(BigInteger.TEN);
    length++;
return length;
BigIntegerlistToBigInteger(List list)
Transforms a List of Integer s representing non-zero bits into a BigInteger of the represented value.
if (list == null || list.isEmpty()) {
    return BigInteger.ZERO;
list.sort((i1, i2) -> i1 > i2 ? 1 : -1);
int max = list.get(list.size() - 1);
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < max; ++i) {
    buffer.append('0');
...
intlog2(BigInteger x)
log
return x.bitLength() - 1;
BigIntegermaskBits(BigInteger value, int bits)
mask Bits
BigInteger mask = BigInteger.ONE.shiftLeft(bits).subtract(BigInteger.ONE);
return value.and(mask);
BigIntegermin(BigInteger a, BigInteger b)
Since Math.max() doesn't handle BigInteger
if (a == null)
    return b;
if (b == null)
    return a;
int c = a.compareTo(b);
return c > 0 ? b : a;