Example usage for java.math BigInteger shiftRight

List of usage examples for java.math BigInteger shiftRight

Introduction

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

Prototype

public BigInteger shiftRight(int n) 

Source Link

Document

Returns a BigInteger whose value is (this >> n) .

Usage

From source file:com.bigdata.dastor.utils.FBUtilities.java

/**
 * Given two bit arrays represented as BigIntegers, containing the given
 * number of significant bits, calculate a midpoint.
 *
 * @param left The left point./*  w  w w.  j a v a2  s  . com*/
 * @param right The right point.
 * @param sigbits The number of bits in the points that are significant.
 * @return A midpoint that will compare bitwise halfway between the params, and
 * a boolean representing whether a non-zero lsbit remainder was generated.
 */
public static Pair<BigInteger, Boolean> midpoint(BigInteger left, BigInteger right, int sigbits) {
    BigInteger midpoint;
    boolean remainder;
    if (left.compareTo(right) < 0) {
        BigInteger sum = left.add(right);
        remainder = sum.testBit(0);
        midpoint = sum.shiftRight(1);
    } else {
        BigInteger max = TWO.pow(sigbits);
        // wrapping case
        BigInteger distance = max.add(right).subtract(left);
        remainder = distance.testBit(0);
        midpoint = distance.shiftRight(1).add(left).mod(max);
    }
    return new Pair(midpoint, remainder);
}

From source file:Util.java

public static BigInteger bitwiseGcd(BigInteger u, BigInteger v) {
    if (u.equals(BigInteger.ZERO))
        return v;
    if (v.equals(BigInteger.ZERO))
        return u;

    //      System.out.format("u=%s=%s\nu.getLowestSetBit()=%s\n%s>>%s=%s = %s\n", u, u.toString(2), u.getLowestSetBit(), u, u.getLowestSetBit(), u.shiftRight(u.getLowestSetBit()), u.shiftRight(u.getLowestSetBit()).toString(2));

    int uBit = u.getLowestSetBit();
    int vBit = v.getLowestSetBit();
    int k = (uBit <= vBit ? uBit : vBit);

    while (u.signum() > 0) {
        // first ensure that both are odd
        u = u.shiftRight(u.getLowestSetBit());
        v = v.shiftRight(v.getLowestSetBit());
        if (u.subtract(v).signum() >= 0) {
            u = (u.subtract(v)).shiftRight(1);
        } else {//from   ww w . java 2 s. co m
            v = (v.subtract(u)).shiftRight(1);
        }
    }

    return v.shiftLeft(k);
}

From source file:Main.java

/**
 * Computes the Window NAF (non-adjacent Form) of an integer.
 * @param width The width <code>w</code> of the Window NAF. The width is
 * defined as the minimal number <code>w</code>, such that for any
 * <code>w</code> consecutive digits in the resulting representation, at
 * most one is non-zero.//from  w  w  w.  j  ava 2 s.c om
 * @param k The integer of which the Window NAF is computed.
 * @return The Window NAF of the given width, such that the following holds:
 * <code>k = &sum;<sub>i=0</sub><sup>l-1</sup> k<sub>i</sub>2<sup>i</sup>
 * </code>, where the <code>k<sub>i</sub></code> denote the elements of the
 * returned <code>byte[]</code>.
 */
public static byte[] generateWindowNaf(int width, BigInteger k) {
    if (width == 2) {
        return generateNaf(k);
    }

    if (width < 2 || width > 8) {
        throw new IllegalArgumentException("'width' must be in the range [2, 8]");
    }

    byte[] wnaf = new byte[k.bitLength() + 1];

    // 2^width and a mask and sign bit set accordingly
    int pow2 = 1 << width;
    int mask = pow2 - 1;
    int sign = pow2 >>> 1;

    boolean carry = false;
    int length = 0, pos = 0;

    while (pos <= k.bitLength()) {
        if (k.testBit(pos) == carry) {
            ++pos;
            continue;
        }

        k = k.shiftRight(pos);

        int digit = k.intValue() & mask;
        if (carry) {
            ++digit;
        }

        carry = (digit & sign) != 0;
        if (carry) {
            digit -= pow2;
        }

        length += (length > 0) ? pos - 1 : pos;
        wnaf[length++] = (byte) digit;
        pos = width;
    }

    // Reduce the WNAF array to its actual length
    if (wnaf.length > length) {
        wnaf = trim(wnaf, length);
    }

    return wnaf;
}

From source file:Main.java

/**
 * Computes the Window NAF (non-adjacent Form) of an integer.
 * @param width The width <code>w</code> of the Window NAF. The width is
 * defined as the minimal number <code>w</code>, such that for any
 * <code>w</code> consecutive digits in the resulting representation, at
 * most one is non-zero.//from  ww  w  .  ja va2  s.c  o m
 * @param k The integer of which the Window NAF is computed.
 * @return The Window NAF of the given width, such that the following holds:
 * <code>k = &sum;<sub>i=0</sub><sup>l-1</sup> k<sub>i</sub>2<sup>i</sup>
 * </code>, where the <code>k<sub>i</sub></code> denote the elements of the
 * returned <code>byte[]</code>.
 */
public static byte[] generateWindowNaf(int width, BigInteger k) {
    if (width == 2) {
        return generateNaf(k);
    }

    if (width < 2 || width > 8) {
        throw new IllegalArgumentException("'width' must be in the range [2, 8]");
    }
    if (k.signum() == 0) {
        return EMPTY_BYTES;
    }

    byte[] wnaf = new byte[k.bitLength() + 1];

    // 2^width and a mask and sign bit set accordingly
    int pow2 = 1 << width;
    int mask = pow2 - 1;
    int sign = pow2 >>> 1;

    boolean carry = false;
    int length = 0, pos = 0;

    while (pos <= k.bitLength()) {
        if (k.testBit(pos) == carry) {
            ++pos;
            continue;
        }

        k = k.shiftRight(pos);

        int digit = k.intValue() & mask;
        if (carry) {
            ++digit;
        }

        carry = (digit & sign) != 0;
        if (carry) {
            digit -= pow2;
        }

        length += (length > 0) ? pos - 1 : pos;
        wnaf[length++] = (byte) digit;
        pos = width;
    }

    // Reduce the WNAF array to its actual length
    if (wnaf.length > length) {
        wnaf = trim(wnaf, length);
    }

    return wnaf;
}

From source file:Main.java

public static byte[] generateJSF(BigInteger g, BigInteger h) {
    int digits = Math.max(g.bitLength(), h.bitLength()) + 1;
    byte[] jsf = new byte[digits];

    BigInteger k0 = g, k1 = h;
    int j = 0, d0 = 0, d1 = 0;

    while (k0.signum() > 0 || k1.signum() > 0 || d0 > 0 || d1 > 0) {
        int n0 = (k0.intValue() + d0) & 7, n1 = (k1.intValue() + d1) & 7;

        int u0 = n0 & 1;
        if (u0 != 0) {
            u0 -= (n0 & 2);//from ww w  .jav  a2 s.  c o m
            if ((n0 + u0) == 4 && (n1 & 3) == 2) {
                u0 = -u0;
            }
        }

        int u1 = n1 & 1;
        if (u1 != 0) {
            u1 -= (n1 & 2);
            if ((n1 + u1) == 4 && (n0 & 3) == 2) {
                u1 = -u1;
            }
        }

        if ((d0 << 1) == 1 + u0) {
            d0 = 1 - d0;
        }
        if ((d1 << 1) == 1 + u1) {
            d1 = 1 - d1;
        }

        k0 = k0.shiftRight(1);
        k1 = k1.shiftRight(1);

        jsf[j++] = (byte) ((u0 << 4) | (u1 & 0xF));
    }

    // Reduce the JSF array to its actual length
    if (jsf.length > j) {
        jsf = trim(jsf, j);
    }

    return jsf;
}

From source file:Main.java

public static int[] generateCompactWindowNaf(int width, BigInteger k) {
    if (width == 2) {
        return generateCompactNaf(k);
    }//  w ww  .j a  v a 2  s . com

    if (width < 2 || width > 16) {
        throw new IllegalArgumentException("'width' must be in the range [2, 16]");
    }
    if ((k.bitLength() >>> 16) != 0) {
        throw new IllegalArgumentException("'k' must have bitlength < 2^16");
    }

    int[] wnaf = new int[k.bitLength() / width + 1];

    // 2^width and a mask and sign bit set accordingly
    int pow2 = 1 << width;
    int mask = pow2 - 1;
    int sign = pow2 >>> 1;

    boolean carry = false;
    int length = 0, pos = 0;

    while (pos <= k.bitLength()) {
        if (k.testBit(pos) == carry) {
            ++pos;
            continue;
        }

        k = k.shiftRight(pos);

        int digit = k.intValue() & mask;
        if (carry) {
            ++digit;
        }

        carry = (digit & sign) != 0;
        if (carry) {
            digit -= pow2;
        }

        int zeroes = length > 0 ? pos - 1 : pos;
        wnaf[length++] = (digit << 16) | zeroes;
        pos = width;
    }

    // Reduce the WNAF array to its actual length
    if (wnaf.length > length) {
        wnaf = trim(wnaf, length);
    }

    return wnaf;
}

From source file:com.wms.utils.DataUtil.java

public static boolean checkValidateIPv4(String fromIPAddress, String toIPAddress, int mask) {

    BigInteger fromIP = ipv4ToNumber(fromIPAddress);
    BigInteger toIP = ipv4ToNumber(toIPAddress);
    BigInteger subnet = new BigInteger("FFFFFFFF", 16);

    fromIP = fromIP.shiftRight(32 - mask).shiftLeft(32 - mask);
    subnet = subnet.shiftRight(mask);//from w  ww  .  jav a 2s .  c  om

    BigInteger broadcastIP = fromIP.xor(subnet);

    if (toIP.compareTo(broadcastIP) == 1) {
        return false;
    }

    return true;
}

From source file:com.wms.utils.DataUtil.java

public static String numberToIpv4(BigInteger ipNumber) {

    String ipString = "";
    BigInteger a = new BigInteger("FF", 16);

    for (int i = 0; i < 4; i++) {
        ipString = ipNumber.and(a).toString() + "." + ipString;

        ipNumber = ipNumber.shiftRight(8);
    }//from  w  w  w. j a  v  a  2s  .c o m

    return ipString.substring(0, ipString.length() - 1);
}

From source file:com.wms.utils.DataUtil.java

public static String numberToIPv6(BigInteger ipNumber) {
    String ipString = "";
    BigInteger a = new BigInteger("FFFF", 16);

    for (int i = 0; i < 8; i++) {
        ipString = ipNumber.and(a).toString(16) + ":" + ipString;

        ipNumber = ipNumber.shiftRight(16);
    }/*from w  w w  .  j a  va2 s .  c  om*/

    return ipString.substring(0, ipString.length() - 1);

}

From source file:Main.java

public static int[] generateCompactWindowNaf(int width, BigInteger k) {
    if (width == 2) {
        return generateCompactNaf(k);
    }/*from  w  ww.jav a2 s .com*/

    if (width < 2 || width > 16) {
        throw new IllegalArgumentException("'width' must be in the range [2, 16]");
    }
    if ((k.bitLength() >>> 16) != 0) {
        throw new IllegalArgumentException("'k' must have bitlength < 2^16");
    }
    if (k.signum() == 0) {
        return EMPTY_INTS;
    }

    int[] wnaf = new int[k.bitLength() / width + 1];

    // 2^width and a mask and sign bit set accordingly
    int pow2 = 1 << width;
    int mask = pow2 - 1;
    int sign = pow2 >>> 1;

    boolean carry = false;
    int length = 0, pos = 0;

    while (pos <= k.bitLength()) {
        if (k.testBit(pos) == carry) {
            ++pos;
            continue;
        }

        k = k.shiftRight(pos);

        int digit = k.intValue() & mask;
        if (carry) {
            ++digit;
        }

        carry = (digit & sign) != 0;
        if (carry) {
            digit -= pow2;
        }

        int zeroes = length > 0 ? pos - 1 : pos;
        wnaf[length++] = (digit << 16) | zeroes;
        pos = width;
    }

    // Reduce the WNAF array to its actual length
    if (wnaf.length > length) {
        wnaf = trim(wnaf, length);
    }

    return wnaf;
}