Example usage for java.math BigInteger bitLength

List of usage examples for java.math BigInteger bitLength

Introduction

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

Prototype

public int bitLength() 

Source Link

Document

Returns the number of bits in the minimal two's-complement representation of this BigInteger, excluding a sign bit.

Usage

From source file:Main.java

public static void main(String[] args) {

    BigInteger bi1 = new BigInteger("7");
    BigInteger bi2 = new BigInteger("-7");

    // perform bitlength operation on bi1, bi2
    int i1 = bi1.bitLength();
    int i2 = bi2.bitLength();

    System.out.println(i1);/*from w w  w . j ava 2  s  .c  o  m*/
    System.out.println(i2);
}

From source file:BigNumApp.java

public static void main(String args[]) {
    BigInteger n = new BigInteger("1000000000000");
    BigInteger one = new BigInteger("1");
    while (!n.isProbablePrime(7))
        n = n.add(one);/* ww  w .  ja  va 2s .  co  m*/
    System.out.println(n.toString(10) + " is probably prime.");
    System.out.println("It is " + n.bitLength() + " bits in length.");
}

From source file:Main.java

public static void main(String[] args) {
    BigInteger bigInteger = new BigInteger("2000000000000");// uper limit
    BigInteger min = new BigInteger("1000000000");// lower limit
    BigInteger bigInteger1 = bigInteger.subtract(min);
    Random rnd = new Random();
    int maxNumBitLength = bigInteger.bitLength();

    BigInteger aRandomBigInt;/*from   w  w w.  j ava 2  s. c  o  m*/

    aRandomBigInt = new BigInteger(maxNumBitLength, rnd);
    if (aRandomBigInt.compareTo(min) < 0)
        aRandomBigInt = aRandomBigInt.add(min);
    if (aRandomBigInt.compareTo(bigInteger) >= 0)
        aRandomBigInt = aRandomBigInt.mod(bigInteger1).add(min);

    System.out.println(aRandomBigInt);
}

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 ww  w  .ja v a 2  s. c om

    return naf;
}

From source file:Main.java

public static int hammingWeight(BigInteger value) {
    int weight = 0;
    for (int i = 0; i <= value.bitLength(); i++) {
        if (value.testBit(i))
            weight++;//from  ww w.  ja  va  2  s  . co m
    }
    return weight;
}

From source file:Main.java

public static BigInteger createPrimeBigger(BigInteger valueThatDeterminesNumberOfBits) {
    int numbits = valueThatDeterminesNumberOfBits.bitLength() + 1;
    Random random = new SecureRandom();
    BigInteger ret = BigInteger.probablePrime(numbits, random);
    return ret;//from  w w w .  j a  va  2  s .c o m
}

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  www .j  a  v  a  2s. c o  m*/

    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 byte[] generateNaf(BigInteger k) {
    if (k.signum() == 0) {
        return EMPTY_BYTES;
    }//  www . j av a 2s .  c  om

    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 createPrimeBigger(BigInteger valueThatDeterminesNumberOfBits, Random random) {
    int numbits = valueThatDeterminesNumberOfBits.bitLength() + 1;

    BigInteger ret = BigInteger.probablePrime(numbits, random);
    return ret;//www  .j  a  va2s  .  c o  m
}

From source file:Main.java

/**
 * Return the value of <tt>big</tt> as a byte array. Although BigInteger
 * has such a method, it uses an extra bit to indicate the sign of the
 * number. For elliptic curve cryptography, the numbers usually are
 * positive. Thus, this helper method returns a byte array of minimal
 * length, ignoring the sign of the number.
 *
 * @param value the <tt>BigInteger</tt> value to be converted to a byte
 *              array/*from   w  w w  .  j  a  v a 2s  .  c om*/
 * @return the value <tt>big</tt> as byte array
 */
public static byte[] toMinimalByteArray(BigInteger value) {
    byte[] valBytes = value.toByteArray();
    if ((valBytes.length == 1) || (value.bitLength() & 0x07) != 0) {
        return valBytes;
    }
    byte[] result = new byte[value.bitLength() >> 3];
    System.arraycopy(valBytes, 1, result, 0, result.length);
    return result;
}