Example usage for java.math BigInteger add

List of usage examples for java.math BigInteger add

Introduction

In this page you can find the example usage for java.math BigInteger add.

Prototype

BigInteger add(long val) 

Source Link

Document

Package private methods used by BigDecimal code to add a BigInteger with a long.

Usage

From source file:Main.java

public static BigInteger byteArrayToBigInteger(byte[] b, int offset, int length) {
    if (b.length < offset + length) {
        throw new IllegalArgumentException("offset + length must be less than or equal to b.length");
    }/*from w w w .  j  av  a2 s.  c  om*/

    BigInteger value = BigInteger.valueOf(0);
    for (int i = 0; i < length; i++) {
        value = value.shiftLeft(8);
        value = value.add(BigInteger.valueOf(b[i + offset] & 0x000000ff));
    }
    return value;
}

From source file:biz.karms.sinkit.ejb.util.CIDRUtils.java

public static ImmutablePair<String, String> getStartEndAddresses(final String cidr)
        throws UnknownHostException {
    //TODO: This is silly. Refactor CIDRUtils so as to accept actual IPs as well as subnets.
    //TODO: Validate the thing before processing. Guava?
    final String fixedCIDR;
    if (!cidr.contains("/")) {
        //IPv6? Hmmm...
        if (cidr.contains(":")) {
            fixedCIDR = cidr + "/128";
        } else {//from w  ww  . j  a v a 2s.  c  om
            fixedCIDR = cidr + "/32";
        }
    } else {
        fixedCIDR = cidr;
    }
    final int index = fixedCIDR.indexOf("/");
    final InetAddress inetAddress = InetAddress.getByName(fixedCIDR.substring(0, index));
    final int prefixLength = Integer.parseInt(fixedCIDR.substring(index + 1));

    final ByteBuffer maskBuffer;
    if (inetAddress.getAddress().length == 4) {
        maskBuffer = ByteBuffer.allocate(4).putInt(-1);
    } else {
        maskBuffer = ByteBuffer.allocate(16).putLong(-1L).putLong(-1L);
    }

    final BigInteger mask = (new BigInteger(1, maskBuffer.array())).not().shiftRight(prefixLength);
    final ByteBuffer buffer = ByteBuffer.wrap(inetAddress.getAddress());
    final BigInteger ipVal = new BigInteger(1, buffer.array());
    final BigInteger startIp = ipVal.and(mask);
    final BigInteger endIp = startIp.add(mask.not());

    return new ImmutablePair<>(String.format("%040d", startIp), String.format("%040d", endIp));
}

From source file:edu.hku.sdb.udf.util.UDFHandler.java

/**
 * Returns (ae + be) mod n according to SDB Addition (EE Mode) protocol
 *
 * @param a  ae, whose value < n/*from  w ww .j  a v a 2s . co  m*/
 * @param b  be, whose value < n
 * @param n
 * @return
 */
public static BigInteger add(BigInteger a, BigInteger b, BigInteger n) {
    return (a.add(b)).mod(n);
}

From source file:edu.hku.sdb.udf.util.UDFHandler.java

/**
 * Simple BigInteger addition for row ID.
 *
 * @param rowID1/*from  w w w  .  j  a  v a 2s. co m*/
 * @param rowID2
 * @return rowID1 + rowID2
 */
public static BigInteger integerAdd(BigInteger rowID1, BigInteger rowID2, BigInteger n) {
    return rowID1.add(rowID2).mod(n);
}

From source file:org.apache.accumulo.server.master.tableOps.Utils.java

static String getNextTableId(String tableName, Instance instance) throws ThriftTableOperationException {

    String tableId = null;/*from  ww  w  .j  a  va  2  s  .co  m*/
    try {
        IZooReaderWriter zoo = ZooReaderWriter.getRetryingInstance();
        final String ntp = ZooUtil.getRoot(instance) + Constants.ZTABLES;
        byte[] nid = zoo.mutate(ntp, "0".getBytes(), ZooUtil.PUBLIC, new Mutator() {
            @Override
            public byte[] mutate(byte[] currentValue) throws Exception {
                BigInteger nextId = new BigInteger(new String(currentValue), Character.MAX_RADIX);
                nextId = nextId.add(BigInteger.ONE);
                return nextId.toString(Character.MAX_RADIX).getBytes();
            }
        });
        return new String(nid);
    } catch (Exception e1) {
        Logger.getLogger(CreateTable.class).error("Failed to assign tableId to " + tableName, e1);
        throw new ThriftTableOperationException(tableId, tableName, TableOperation.CREATE,
                TableOperationExceptionType.OTHER, e1.getMessage());
    }
}

From source file:cc.redberry.core.number.NumberUtils.java

private static boolean isSqrtXXX(BigInteger n, BigInteger root) {
    final BigInteger lowerBound = root.pow(2);
    final BigInteger upperBound = root.add(BigInteger.ONE).pow(2);
    return lowerBound.compareTo(n) <= 0 && n.compareTo(upperBound) < 0;
}

From source file:Main.java

/** find a palindromic number given a starting point, by
 * calling ourself until we get a number that is palindromic.
 *//*from   w  w w.  j av  a2  s.  c o m*/
public static BigInteger findPalindrome(BigInteger num) {
    if (num.compareTo(BigInteger.ZERO) < 0)
        throw new IllegalStateException("negative");
    if (isPalindrome(num))
        return num;
    if (verbose)
        System.out.println("Trying " + num);
    return findPalindrome(num.add(reverseNumber(num)));
}

From source file:cc.redberry.core.number.NumberUtils.java

/**
 * Computes the integer square root of a number.
 *
 * @param n The number./*  w w w .  j a v a2s . co m*/
 * @return The integer square root, i.e. the largest number whose square
 *         doesn't exceed n.
 */
public static BigInteger sqrt(BigInteger n) {
    if (n.signum() >= 0) {
        final int bitLength = n.bitLength();
        BigInteger root = BigInteger.ONE.shiftLeft(bitLength / 2);

        while (!isSqrtXXX(n, root))
            root = root.add(n.divide(root)).divide(TWO);
        return root;
    } else
        throw new ArithmeticException("square root of negative number");
}

From source file:jp.co.ntts.vhut.util.MacConversionUtil.java

/**
 * ?????????./*ww w  . j  a va2 s . co m*/
 * @param macStart .
 * @param count ?
 * @return 
 */
public static byte[] getMacAddressWithOrderAsByte(String macStart, int count) {
    BigInteger biMacStart = new BigInteger(addrToByte(macStart));
    BigInteger biSub = BigInteger.valueOf(count);
    byte[] bMacTarget = biMacStart.add(biSub).toByteArray();
    byte[] result = new byte[6];
    int delta = 6 - bMacTarget.length;
    for (int i = 0; i < 6; i++) {
        if (i >= delta) {
            result[i] = bMacTarget[i - delta];
        } else {
            result[i] = (byte) 0;
        }
    }
    return result;
}

From source file:org.apache.hadoop.crypto.TestCryptoCodec.java

private static BigInteger calculateRef(byte[] initIV, long counter) {
    byte[] cb = Longs.toByteArray(counter);
    BigInteger bi = new BigInteger(1, initIV);
    return bi.add(new BigInteger(1, cb));
}