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

byte[]getBytes(BigInteger big, int bitlen)
Returns a byte-array representation of a BigInteger .
bitlen = ((bitlen + 7) >> 3) << 3;
if (bitlen < big.bitLength()) {
    throw new IllegalArgumentException("IllegalBitlength");
byte[] bigBytes = big.toByteArray();
if (((big.bitLength() % 8) != 0) && (((big.bitLength() / 8) + 1) == (bitlen / 8))) {
    return bigBytes;
int startSrc = 0; 
int bigLen = bigBytes.length; 
if ((big.bitLength() % 8) == 0) { 
    startSrc = 1; 
    bigLen--; 
int startDst = bitlen / 8 - bigLen; 
byte[] resizedBytes = new byte[bitlen / 8];
System.arraycopy(bigBytes, startSrc, resizedBytes, startDst, bigLen);
return resizedBytes;
byte[]getBytesWithoutSign(BigInteger arg)
get Bytes Without Sign
byte[] sourceArray = arg.toByteArray();
if (sourceArray[0] != 0) {
    return sourceArray;
} else {
    byte[] withoutSign = new byte[sourceArray.length - 1];
    System.arraycopy(sourceArray, 1, withoutSign, 0, withoutSign.length);
    return withoutSign;
BigInteger[]getClearExpPipe(BigInteger M)
Returns the numbers: M, M^2, M^3, ..., M^maxExp
BigInteger[] Ms = new BigInteger[EXP_PIPE_SIZE - 1];
Ms[0] = M;
for (int i = 1; i < Ms.length; i++) {
    Ms[i] = Ms[i - 1].multiply(M).mod(p);
return Ms;
StringgetCreateLocalNextHopJobKey(Long vpnId, BigInteger dpnId, String prefix)
get Create Local Next Hop Job Key
return "FIB-" + vpnId.toString() + "-" + dpnId.toString() + "-" + prefix;
GregorianCalendargetDateOf(BigInteger fileTime)
Since date values in asf files are given in 100 ns steps since first january of 1601 a little conversion must be done.
GregorianCalendar result = new GregorianCalendar(1601, 0, 1);
fileTime = fileTime.divide(new BigInteger("10000")); 
BigInteger maxInt = new BigInteger(String.valueOf(Integer.MAX_VALUE));
while (fileTime.compareTo(maxInt) > 0) {
    result.add(Calendar.MILLISECOND, Integer.MAX_VALUE);
    fileTime = fileTime.subtract(maxInt);
result.add(Calendar.MILLISECOND, fileTime.intValue());
...
GregorianCalendargetDateOf(final BigInteger fileTime)
Date values in ASF files are given in 100 ns (10 exp -4) steps since first
final GregorianCalendar result = new GregorianCalendar();
BigInteger time = fileTime.divide(new BigInteger("10"));
Date date = new Date(time.longValue() - DIFF_BETWEEN_ASF_DATE_AND_JAVA_DATE);
result.setTime(date);
return result;
intgetDigitCount(BigInteger number)
get Digit Count
double factor = Math.log(2) / Math.log(10);
int digitCount = (int) (factor * number.bitLength() + 1);
if (BigInteger.TEN.pow(digitCount - 1).compareTo(number) > 0) {
    return digitCount - 1;
return digitCount;
StringgetHexString(BigInteger bigInt)
Get hex string for the supplied big integer: "0x" where hex string is outputted in groups of exactly four characters sub-divided by spaces.
String hex = bigInt.toString(16).toUpperCase();
int padding = (4 - (hex.length() % 4));
if ((padding > 0) && (padding < 4)) {
    StringBuilder sb = new StringBuilder(hex);
    for (int i = 0; i < padding; i++) {
        sb.insert(0, '0');
    hex = sb.toString();
...
ListgetHistogramBigInt(List data, int breaks)
Same as getHistogram but operates on BigIntegers.
if (data.isEmpty()) {
    return Collections.emptyList();
List<Integer> ret = new ArrayList<Integer>(breaks);
for (int i = 0; i < breaks; i++) {
    ret.add(0);
BigInteger min = Collections.min(data);
...
BigIntegergetIntegral(String number, BigInteger def)
Parse the given string into a big integer if possible otherwise return the specified default.
try {
    return new BigInteger(number);
} catch (Exception e) {
    return def;