List of usage examples for java.math BigInteger shiftRight
public BigInteger shiftRight(int n)
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.shiftRight(1); }
From source file:Main.java
public static void main(String[] args) { BigInteger bi1 = new BigInteger("4"); // perform right shift operation on bi1 using 2 and -2 BigInteger bi2 = bi1.shiftRight(2); BigInteger bi3 = bi1.shiftRight(-2); System.out.println(bi2);//from ww w . java 2s . co m System.out.println(bi3); }
From source file:Main.java
public static BigInteger modNear(BigInteger a, BigInteger b) { BigInteger res = a.mod(b);/*from w w w . j av a 2 s .com*/ if (res.compareTo(b.shiftRight(1)) == 1) res = res.subtract(b); return res; }
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); // Do the tests if (rnd == null) { rnd = new SecureRandom(); }/*ww w .j a v a 2s. com*/ 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:Main.java
private static BigInteger findSquareRoot(BigInteger alpha, BigInteger p) { BigInteger beta = null;//from ww w . j a v a2 s.c o m if (p.mod(BigInteger.valueOf(4)).compareTo(BigInteger.valueOf(3)) == 0) { BigInteger k = p.shiftRight(2).add(ONE); beta = alpha.modPow(k, p); } else if (p.mod(BigInteger.valueOf(8)).compareTo(BigInteger.valueOf(5)) == 0) { System.out.println("p = 8 mod 5"); BigInteger k = p.subtract(BigInteger.valueOf(5)).divide(BigInteger.valueOf(8)); BigInteger gamma = alpha.multiply(BigInteger.valueOf(2)).modPow(k, p); BigInteger i = alpha.multiply(BigInteger.valueOf(2)).multiply(gamma.pow(2)).mod(p); beta = alpha.multiply(gamma).multiply(i.subtract(ONE)).mod(p); } else if (p.mod(BigInteger.valueOf(8)).compareTo(BigInteger.valueOf(1)) == 0) { beta = null; //TODO System.out.println("finding square root not fully implemented yet"); } return beta; }
From source file:Main.java
/** * Convert to base 128 (bigendian), using shifts. * @param val//from ww w . jav a 2s .c om * @return */ public static ArrayList<Integer> base128(BigInteger val) { ArrayList<Integer> result = new ArrayList<Integer>(); int part = val.and(BN127).intValue(); val = val.shiftRight(7); result.add(0, new Integer(part)); while (!val.equals(BigInteger.ZERO)) { part = val.and(BN127).intValue(); val = val.shiftRight(7); part += 128; result.add(0, new Integer(part)); } ; return result; }
From source file:Main.java
/** * Compute the square root of x to a given scale, x >= 0. Use Newton's * algorithm./* w ww . j a v a2s.co m*/ * * @param x * the value of x * @return the result value */ public static BigDecimal sqrt(BigDecimal x) { // Check that x >= 0. if (x.signum() < 0) { throw new ArithmeticException("x < 0"); } // n = x*(10^(2*SCALE)) BigInteger n = x.movePointRight(SCALE << 1).toBigInteger(); // The first approximation is the upper half of n. int bits = (n.bitLength() + 1) >> 1; BigInteger ix = n.shiftRight(bits); BigInteger ixPrev; // Loop until the approximations converge // (two successive approximations are equal after rounding). do { ixPrev = ix; // x = (x + n/x)/2 ix = ix.add(n.divide(ix)).shiftRight(1); Thread.yield(); } while (ix.compareTo(ixPrev) != 0); return new BigDecimal(ix, SCALE); }
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 w w w. j a v a2 s . c o m*/ exponent = exponent.shiftRight(1); } return result; }
From source file:Main.java
static BigInteger mulmod(BigInteger a, BigInteger b, BigInteger p) { BigInteger r = BigInteger.ZERO; while (b.compareTo(BigInteger.ZERO) > 0) { if (!b.and(BigInteger.ONE).equals(BigInteger.ZERO)) { r = addmod(r, a, p);/*from w ww. j a v a 2 s . c om*/ } b = b.shiftRight(1); a = addmod(a, a, p); } return r; }
From source file:com.clustercontrol.repository.util.RepositoryUtil.java
/** * BigIntegerIP(IPv6)?????/*from w ww . j av a2s .com*/ * @param argInt IPv6? * @return String */ public static String bigIntToIpV6(BigInteger argInt) { StringBuilder str = new StringBuilder(); for (int i = 15; i >= 0; i--) { int shift = 8 * i; Integer n = 0xff; BigInteger num = argInt.shiftRight(shift).and(new BigInteger(n.toString())); int intNum = num.intValue(); String s = Integer.toHexString(intNum); if (s.length() < 2) { s = "0" + s; } str.append(s); if (i > 0 && i < 15) { int f = i % 2; str.append(f == 0 ? ":" : ""); } } return str.toString(); }