List of usage examples for java.math BigInteger getLowestSetBit
public int getLowestSetBit()
From source file:Main.java
public static void main(String[] args) { BigInteger bi1 = new BigInteger("8");// 1000 BigInteger bi2 = new BigInteger("7");// 0111 // perform getLowestSetBit on bi1, bi2 int i1 = bi1.getLowestSetBit(); int i2 = bi2.getLowestSetBit(); System.out.println(i1);//from ww w .ja va2s. c o m System.out.println(i2); }
From source file:Util.java
public static boolean isEven(BigInteger number) { return number.getLowestSetBit() != 0; }
From source file:Main.java
private static boolean passesMillerRabin(BigInteger us, int iterations, Random rnd) { final BigInteger ONE = BigInteger.ONE; final BigInteger TWO = BigInteger.valueOf(2); // Find a and m such that m is odd and this == 1 + 2**a * m BigInteger thisMinusOne = us.subtract(ONE); BigInteger m = thisMinusOne; int a = m.getLowestSetBit(); m = m.shiftRight(a);//from ww w . j a v a 2 s. c o m // Do the tests if (rnd == null) { rnd = new SecureRandom(); } for (int i = 0; i < iterations; i++) { // Generate a uniform random on (1, this) BigInteger b; do { b = new BigInteger(us.bitLength(), rnd); } while (b.compareTo(ONE) <= 0 || b.compareTo(us) >= 0); int j = 0; BigInteger z = b.modPow(m, us); while (!((j == 0 && z.equals(ONE)) || z.equals(thisMinusOne))) { if (j > 0 && z.equals(ONE) || ++j == a) return false; z = z.modPow(TWO, us); } } return true; }
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 w w w. j a v a 2 s . c o m v = (v.subtract(u)).shiftRight(1); } } return v.shiftLeft(k); }