Java Code Examples for java.math.BigInteger#bitCount()

The following examples show how to use java.math.BigInteger#bitCount() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public static void bitCount() {
    int failCount = 0;

    for (int i=0; i<SIZE*10; i++) {
        int x = rnd.nextInt();
        BigInteger bigX = BigInteger.valueOf((long)x);
        int bit = (x < 0 ? 0 : 1);
        int tmp = x, bitCount = 0;
        for (int j=0; j<32; j++) {
            bitCount += ((tmp & 1) == bit ? 1 : 0);
            tmp >>= 1;
        }

        if (bigX.bitCount() != bitCount) {
            //System.err.println(x+": "+bitCount+", "+bigX.bitCount());
            failCount++;
        }
    }
    report("Bit Count", failCount);
}
 
Example 2
public static void bitCount() {
    int failCount = 0;

    for (int i=0; i<SIZE*10; i++) {
        int x = rnd.nextInt();
        BigInteger bigX = BigInteger.valueOf((long)x);
        int bit = (x < 0 ? 0 : 1);
        int tmp = x, bitCount = 0;
        for (int j=0; j<32; j++) {
            bitCount += ((tmp & 1) == bit ? 1 : 0);
            tmp >>= 1;
        }

        if (bigX.bitCount() != bitCount) {
            //System.err.println(x+": "+bitCount+", "+bigX.bitCount());
            failCount++;
        }
    }
    report("Bit Count", failCount);
}
 
Example 3
public static void bitCount() {
    int failCount = 0;

    for (int i=0; i<SIZE*10; i++) {
        int x = rnd.nextInt();
        BigInteger bigX = BigInteger.valueOf((long)x);
        int bit = (x < 0 ? 0 : 1);
        int tmp = x, bitCount = 0;
        for (int j=0; j<32; j++) {
            bitCount += ((tmp & 1) == bit ? 1 : 0);
            tmp >>= 1;
        }

        if (bigX.bitCount() != bitCount) {
            //System.err.println(x+": "+bitCount+", "+bigX.bitCount());
            failCount++;
        }
    }
    report("Bit Count", failCount);
}
 
Example 4
public static void bitCount() {
    int failCount = 0;

    for (int i=0; i<SIZE*10; i++) {
        int x = rnd.nextInt();
        BigInteger bigX = BigInteger.valueOf((long)x);
        int bit = (x < 0 ? 0 : 1);
        int tmp = x, bitCount = 0;
        for (int j=0; j<32; j++) {
            bitCount += ((tmp & 1) == bit ? 1 : 0);
            tmp >>= 1;
        }

        if (bigX.bitCount() != bitCount) {
            //System.err.println(x+": "+bitCount+", "+bigX.bitCount());
            failCount++;
        }
    }
    report("Bit Count", failCount);
}
 
Example 5
/**
 * Generate the next serial number.
 * @param byteLen byte length of the serial number.
 * @return the serial number.
 */
public BigInteger nextSerialNumber(int byteLen) {
  final byte[] rndBytes = new byte[byteLen];
  final int minWeight = byteLen * 2;

  while (true) {
    random.nextBytes(rndBytes);
    // set the first bit to 0.
    rndBytes[0] &= 0x7F;

    // check NAF weight
    BigInteger bi = new BigInteger(rndBytes);

    BigInteger threeBi = bi.shiftLeft(1).add(bi);
    BigInteger diff = threeBi.xor(bi);
    int nafWeight = diff.bitCount();
    if (nafWeight >= minWeight) {
      return bi;
    }
  }

}
 
Example 6
public static void bitCount() {
    int failCount = 0;

    for (int i=0; i<SIZE*10; i++) {
        int x = rnd.nextInt();
        BigInteger bigX = BigInteger.valueOf((long)x);
        int bit = (x < 0 ? 0 : 1);
        int tmp = x, bitCount = 0;
        for (int j=0; j<32; j++) {
            bitCount += ((tmp & 1) == bit ? 1 : 0);
            tmp >>= 1;
        }

        if (bigX.bitCount() != bitCount) {
            //System.err.println(x+": "+bitCount+", "+bigX.bitCount());
            failCount++;
        }
    }
    report("Bit Count", failCount);
}
 
Example 7
public static void bitCount() {
    int failCount = 0;

    for (int i=0; i<SIZE*10; i++) {
        int x = random.nextInt();
        BigInteger bigX = BigInteger.valueOf((long)x);
        int bit = (x < 0 ? 0 : 1);
        int tmp = x, bitCount = 0;
        for (int j=0; j<32; j++) {
            bitCount += ((tmp & 1) == bit ? 1 : 0);
            tmp >>= 1;
        }

        if (bigX.bitCount() != bitCount) {
            //System.err.println(x+": "+bitCount+", "+bigX.bitCount());
            failCount++;
        }
    }
    report("Bit Count", failCount);
}
 
Example 8
public static void bitCount() {
    int failCount = 0;

    for (int i=0; i<SIZE*10; i++) {
        int x = rnd.nextInt();
        BigInteger bigX = BigInteger.valueOf((long)x);
        int bit = (x < 0 ? 0 : 1);
        int tmp = x, bitCount = 0;
        for (int j=0; j<32; j++) {
            bitCount += ((tmp & 1) == bit ? 1 : 0);
            tmp >>= 1;
        }

        if (bigX.bitCount() != bitCount) {
            //System.err.println(x+": "+bitCount+", "+bigX.bitCount());
            failCount++;
        }
    }
    report("Bit Count", failCount);
}
 
Example 9
public static void bitCount() {
    int failCount = 0;

    for (int i=0; i<SIZE*10; i++) {
        int x = rnd.nextInt();
        BigInteger bigX = BigInteger.valueOf((long)x);
        int bit = (x < 0 ? 0 : 1);
        int tmp = x, bitCount = 0;
        for (int j=0; j<32; j++) {
            bitCount += ((tmp & 1) == bit ? 1 : 0);
            tmp >>= 1;
        }

        if (bigX.bitCount() != bitCount) {
            //System.err.println(x+": "+bitCount+", "+bigX.bitCount());
            failCount++;
        }
    }
    report("Bit Count", failCount);
}
 
Example 10
public static void bitCount() {
    int failCount = 0;

    for (int i=0; i<SIZE*10; i++) {
        int x = rnd.nextInt();
        BigInteger bigX = BigInteger.valueOf((long)x);
        int bit = (x < 0 ? 0 : 1);
        int tmp = x, bitCount = 0;
        for (int j=0; j<32; j++) {
            bitCount += ((tmp & 1) == bit ? 1 : 0);
            tmp >>= 1;
        }

        if (bigX.bitCount() != bitCount) {
            //System.err.println(x+": "+bitCount+", "+bigX.bitCount());
            failCount++;
        }
    }
    report("Bit Count", failCount);
}
 
Example 11
public static void bitCount() {
    int failCount = 0;

    for (int i=0; i<SIZE*10; i++) {
        int x = rnd.nextInt();
        BigInteger bigX = BigInteger.valueOf((long)x);
        int bit = (x < 0 ? 0 : 1);
        int tmp = x, bitCount = 0;
        for (int j=0; j<32; j++) {
            bitCount += ((tmp & 1) == bit ? 1 : 0);
            tmp >>= 1;
        }

        if (bigX.bitCount() != bitCount) {
            //System.err.println(x+": "+bitCount+", "+bigX.bitCount());
            failCount++;
        }
    }
    report("Bit Count", failCount);
}
 
Example 12
public static void bitCount() {
    int failCount = 0;

    for (int i=0; i<SIZE*10; i++) {
        int x = rnd.nextInt();
        BigInteger bigX = BigInteger.valueOf((long)x);
        int bit = (x < 0 ? 0 : 1);
        int tmp = x, bitCount = 0;
        for (int j=0; j<32; j++) {
            bitCount += ((tmp & 1) == bit ? 1 : 0);
            tmp >>= 1;
        }

        if (bigX.bitCount() != bitCount) {
            //System.err.println(x+": "+bitCount+", "+bigX.bitCount());
            failCount++;
        }
    }
    report("Bit Count", failCount);
}
 
Example 13
Source Project: ripple-lib-java   File: WNafUtil.java    License: ISC License 5 votes vote down vote up
public static int getNafWeight(BigInteger k)
{
    if (k.signum() == 0)
    {
        return 0;
    }

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

    return diff.bitCount();
}
 
Example 14
Source Project: Hackerrank-Solutions   File: ACMICPCTeam.java    License: MIT License 5 votes vote down vote up
static int[] acmTeam(String[] topic) {

		int n = topic.length;
		BigInteger[] bi = new BigInteger[n];

		for (int i = 0; i < n; i++)
			bi[i] = new BigInteger(topic[i], 2);

		int maxTopic = 0;
		int teamCount = 0;

		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				BigInteger iuj = bi[i].or(bi[j]);
				int bitCount = iuj.bitCount();
				if (bitCount > maxTopic) {
					maxTopic = bitCount;
					teamCount = 1;
				} else if (bitCount == maxTopic) {
					teamCount++;
				}
			}
		}

		int result[] = { maxTopic, teamCount };
		return result;
	}
 
Example 15
Source Project: ECTester   File: ECUtil.java    License: MIT License 5 votes vote down vote up
public static EC_Params fixedRandomPoint(EC_Curve curve) {
    EllipticCurve ecCurve = curve.toCurve();

    BigInteger p;
    if (ecCurve.getField() instanceof ECFieldFp) {
        ECFieldFp fp = (ECFieldFp) ecCurve.getField();
        p = fp.getP();
        if (!p.isProbablePrime(20)) {
            return null;
        }
    } else {
        //TODO
        return null;
    }

    BigInteger x = new BigInteger(1, hashCurve(curve)).mod(p);
    BigInteger rhs = computeRHS(x, ecCurve.getA(), ecCurve.getB(), p);
    while (!isResidue(rhs, p)) {
        x = x.add(BigInteger.ONE).mod(p);
        rhs = computeRHS(x, ecCurve.getA(), ecCurve.getB(), p);
    }
    BigInteger y = modSqrt(rhs, p);
    if (y.bitCount() % 2 == 0) {
        y = p.subtract(y);
    }

    byte[] xArr = toByteArray(x, ecCurve.getField().getFieldSize());
    byte[] yArr = toByteArray(y, ecCurve.getField().getFieldSize());
    return new EC_Params(EC_Consts.PARAMETER_W, new byte[][]{xArr, yArr});
}
 
Example 16
Source Project: client-sdk-java   File: FixedPointType.java    License: Apache License 2.0 4 votes vote down vote up
private static boolean isValidBitCount(int mBitSize, int nBitSize, BigInteger value) {
    return value.bitCount() <= mBitSize + nBitSize;
}
 
Example 17
Source Project: AVM   File: BigIntegerTarget.java    License: MIT License 4 votes vote down vote up
@Callable
public static int bitCountBigInteger(BigInteger testValue) {
    return testValue.bitCount();
}
 
Example 18
Source Project: web3sdk   File: FixedPointType.java    License: Apache License 2.0 4 votes vote down vote up
private static boolean isValidBitCount(int mBitSize, int nBitSize, BigInteger value) {
    return value.bitCount() <= mBitSize + nBitSize;
}
 
Example 19
public static int hammingDistance(final BigInteger a, final BigInteger b) {
    BigInteger xor = a.xor(b);
    return xor.bitCount();
}
 
Example 20
Source Project: web3j   File: FixedPointType.java    License: Apache License 2.0 4 votes vote down vote up
private static boolean isValidBitCount(int mBitSize, int nBitSize, BigInteger value) {
    return value.bitCount() <= mBitSize + nBitSize;
}