List of usage examples for java.math BigInteger testBit
public boolean testBit(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); boolean b = bi.testBit(3); b = bi.testBit(16);//from ww w. j ava 2 s . c o m }
From source file:Main.java
public static void main(String[] args) { BigInteger bi = new BigInteger("10"); Boolean b1 = bi.testBit(2); Boolean b2 = bi.testBit(3);/*from w w w .ja va 2s .c om*/ // print b1, b2 values System.out.println(b1); System.out.println(b2); }
From source file:Main.java
public static byte[] generateNaf(BigInteger k) { if (k.signum() == 0) { return EMPTY_BYTES; }// w w w.j a v a2s. 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: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 v a2 s . c o m*/ } return weight; }
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 ava2s . co m exponent = exponent.shiftRight(1); } return result; }
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); }/* ww w .ja va 2 s .c o m*/ 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"); }// ww w . j a v a 2 s. c o m 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; }
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"); }/*ww w. j a va 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:com.bigdata.dastor.utils.FBUtilities.java
/** * Given two bit arrays represented as BigIntegers, containing the given * number of significant bits, calculate a midpoint. * * @param left The left point.// www .j a v a 2s .c o m * @param right The right point. * @param sigbits The number of bits in the points that are significant. * @return A midpoint that will compare bitwise halfway between the params, and * a boolean representing whether a non-zero lsbit remainder was generated. */ public static Pair<BigInteger, Boolean> midpoint(BigInteger left, BigInteger right, int sigbits) { BigInteger midpoint; boolean remainder; if (left.compareTo(right) < 0) { BigInteger sum = left.add(right); remainder = sum.testBit(0); midpoint = sum.shiftRight(1); } else { BigInteger max = TWO.pow(sigbits); // wrapping case BigInteger distance = max.add(right).subtract(left); remainder = distance.testBit(0); midpoint = distance.shiftRight(1).add(left).mod(max); } return new Pair(midpoint, remainder); }
From source file:org.apache.cassandra.utils.FBUtilities.java
/** * Given two bit arrays represented as BigIntegers, containing the given * number of significant bits, calculate a midpoint. * * @param left The left point.//from w ww . j a v a 2 s . co m * @param right The right point. * @param sigbits The number of bits in the points that are significant. * @return A midpoint that will compare bitwise halfway between the params, and * a boolean representing whether a non-zero lsbit remainder was generated. */ public static Pair<BigInteger, Boolean> midpoint(BigInteger left, BigInteger right, int sigbits) { BigInteger midpoint; boolean remainder; if (left.compareTo(right) < 0) { BigInteger sum = left.add(right); remainder = sum.testBit(0); midpoint = sum.shiftRight(1); } else { BigInteger max = TWO.pow(sigbits); // wrapping case BigInteger distance = max.add(right).subtract(left); remainder = distance.testBit(0); midpoint = distance.shiftRight(1).add(left).mod(max); } return new Pair<BigInteger, Boolean>(midpoint, remainder); }