Example usage for java.math BigInteger shiftLeft

List of usage examples for java.math BigInteger shiftLeft

Introduction

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

Prototype

public BigInteger shiftLeft(int n) 

Source Link

Document

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

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    byte[] bytes = new byte[] { 0x1, 0x00, 0x00 };
    BigInteger bi = new BigInteger(bytes);
    bi = bi.shiftLeft(3);

}

From source file:Main.java

public static void main(String[] args) {

    BigInteger bi1 = new BigInteger("10");

    // perform leftshift operation on bi1 using 2 and -2
    BigInteger bi2 = bi1.shiftLeft(2);
    BigInteger bi3 = bi1.shiftLeft(-2);

    System.out.println(bi2);/*from www.  j ava  2  s.  c o m*/
    System.out.println(bi3);
}

From source file:Main.java

public static byte[] generateNaf(BigInteger k) {
    BigInteger _3k = k.shiftLeft(1).add(k);

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

    for (int i = 1; i <= digits; ++i) {
        boolean _3kBit = _3k.testBit(i);
        boolean kBit = k.testBit(i);

        naf[i - 1] = (byte) (_3kBit == kBit ? 0 : kBit ? -1 : 1);
    }//from   www  .  ja v  a  2  s.  c  o m

    return naf;
}

From source file:Main.java

public static int getNafWeight(BigInteger k) {
    if (k.signum() == 0) {
        return 0;
    }/*ww w  .j  a  v  a  2  s . com*/
    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 .  com

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

    return diff.bitCount();
}

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.  ja  va2s  . co  m

    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:Main.java

public static byte[] generateNaf(BigInteger k) {
    if (k.signum() == 0) {
        return EMPTY_BYTES;
    }/*from  ww w.ja  va 2 s . c o  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:ID.java

public static String getHexString(byte[] bytes) {
    // This method cannot change even if it's wrong.
    BigInteger bigInteger = BigInteger.ZERO;
    int shift = 0;
    for (int i = bytes.length; --i >= 0;) {
        BigInteger contrib = BigInteger.valueOf(bytes[i] & 0xFF);
        contrib = contrib.shiftLeft(shift);
        bigInteger = bigInteger.add(contrib);
        shift += 8;/*from  ww  w  .  ja v  a2s. c o m*/
    }
    return bigInteger.toString(16).toUpperCase();
}

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 ww  .  j av  a2s  .  com*/

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

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

    int length = 0, zeroes = 0;
    for (int i = 1; i <= digits; ++i) {
        boolean _3kBit = _3k.testBit(i);
        boolean kBit = k.testBit(i);

        if (_3kBit == kBit) {
            ++zeroes;
        } else {
            int digit = kBit ? -1 : 1;
            naf[length++] = (digit << 16) | zeroes;
            zeroes = 0;
        }
    }

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

    return naf;
}

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  2 s.com
    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;
}