Example usage for java.math BigInteger signum

List of usage examples for java.math BigInteger signum

Introduction

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

Prototype

int signum

To view the source code for java.math BigInteger signum.

Click Source Link

Document

The signum of this BigInteger: -1 for negative, 0 for zero, or 1 for positive.

Usage

From source file:Main.java

public static void main(String[] args) {

    BigInteger bi1 = new BigInteger("0");
    BigInteger bi2 = new BigInteger("10");
    BigInteger bi3 = new BigInteger("-10");

    // assign signum results of bi1, bi2, bi3 to i1, i2, i3
    int i1 = bi1.signum();
    int i2 = bi2.signum();
    int i3 = bi3.signum();

    // print i1, i2, i3 values
    System.out.println(i1);//from  ww  w. ja v a 2 s.c om
    System.out.println(i2);
    System.out.println(i3);
}

From source file:Main.java

public static int getNafWeight(BigInteger k) {
    if (k.signum() == 0) {
        return 0;
    }/* w ww .j av  a 2 s . c om*/
    return k.shiftLeft(1).add(k).xor(k).bitCount();
}

From source file:Main.java

public static int getNafWeight(BigInteger k) {
    if (k.signum() == 0) {
        return 0;
    }/* w  w w  .j av  a 2 s . c o m*/

    BigInteger _3k = k.shiftLeft(1).add(k);
    BigInteger diff = _3k.xor(k);

    return diff.bitCount();
}

From source file:Main.java

public static byte[] generateNaf(BigInteger k) {
    if (k.signum() == 0) {
        return EMPTY_BYTES;
    }/*from   w  w w  . j a va  2 s.co m*/

    BigInteger _3k = k.shiftLeft(1).add(k);

    int digits = _3k.bitLength() - 1;
    byte[] naf = new byte[digits];

    BigInteger diff = _3k.xor(k);

    for (int i = 1; i < digits; ++i) {
        if (diff.testBit(i)) {
            naf[i - 1] = (byte) (k.testBit(i) ? -1 : 1);
            ++i;
        }
    }

    naf[digits - 1] = 1;

    return naf;
}

From source file:Main.java

public static BigInteger pow(BigInteger base, BigInteger exponent) {
    BigInteger result = BigInteger.ONE;
    while (exponent.signum() > 0) {
        if (exponent.testBit(0))
            result = result.multiply(base);
        base = base.multiply(base);/*from   www  .j a  v a 2 s.  c  o m*/
        exponent = exponent.shiftRight(1);
    }
    return result;
}

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 .  j  a  v  a2  s.co 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:cc.redberry.core.number.NumberUtils.java

/**
 * Computes the integer square root of a number.
 *
 * @param n The number.//from   w w w  .j a  va  2  s .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:com.google.uzaygezen.core.ranges.BigIntegerRange.java

static BigInteger overlap(List<BigIntegerRange> x, List<BigIntegerRange> y) {
    int n = x.size();
    Preconditions.checkArgument(y.size() == n, "x and y must have the same number of values");
    BigInteger overlap = BigInteger.ONE;
    // Stop early if overlap.signum() becomes zero.
    for (int i = 0; i < n & overlap.signum() != 0; ++i) {
        BigIntegerRange xRange = x.get(i);
        BigIntegerRange yRange = y.get(i);
        overlap = overlap.multiply(xRange.overlap(yRange));
    }//w ww .  j  av  a  2  s  . c o m
    return overlap;
}

From source file:HexUtil.java

/**
  * Write a (reasonably short) BigInteger to a stream.
  * @param integer the BigInteger to write
  * @param out the stream to write it to
  *//*from   w  w w .j a va  2  s . co  m*/
public static void writeBigInteger(BigInteger integer, DataOutputStream out) throws IOException {
    if (integer.signum() == -1) {
        //dump("Negative BigInteger", Logger.ERROR, true);
        throw new IllegalStateException("Negative BigInteger!");
    }
    byte[] buf = integer.toByteArray();
    if (buf.length > Short.MAX_VALUE)
        throw new IllegalStateException("Too long: " + buf.length);
    out.writeShort((short) buf.length);
    out.write(buf);
}

From source file:Main.java

public static int[] generateCompactNaf(BigInteger k) {
    if ((k.bitLength() >>> 16) != 0) {
        throw new IllegalArgumentException("'k' must have bitlength < 2^16");
    }/*from  w w  w.j  a  v a 2s  .  c  om*/
    if (k.signum() == 0) {
        return EMPTY_INTS;
    }

    BigInteger _3k = k.shiftLeft(1).add(k);

    int bits = _3k.bitLength();
    int[] naf = new int[bits >> 1];

    BigInteger diff = _3k.xor(k);

    int highBit = bits - 1, length = 0, zeroes = 0;
    for (int i = 1; i < highBit; ++i) {
        if (!diff.testBit(i)) {
            ++zeroes;
            continue;
        }

        int digit = k.testBit(i) ? -1 : 1;
        naf[length++] = (digit << 16) | zeroes;
        zeroes = 1;
        ++i;
    }

    naf[length++] = (1 << 16) | zeroes;

    if (naf.length > length) {
        naf = trim(naf, length);
    }

    return naf;
}