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

The following examples show how to use java.math.BigInteger#bitCount() . 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
Source File: BigIntegerTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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
Source File: BigIntegerTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
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
Source File: BigIntegerTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
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
Source File: BigIntegerTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
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
Source File: BigIntegerTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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 6
Source File: BigIntegerTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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
Source File: BigIntegerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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
Source File: BigIntegerTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
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
Source File: RandomSerialNumberGenerator.java    From xipki with Apache License 2.0 6 votes vote down vote up
/**
 * 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 10
Source File: BigIntegerTest.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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
Source File: BigIntegerTest.java    From native-obfuscator with GNU General Public License v3.0 6 votes vote down vote up
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
Source File: BigIntegerTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
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 File: ACMICPCTeam.java    From Hackerrank-Solutions with 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 14
Source File: WNafUtil.java    From ripple-lib-java with 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 15
Source File: ECUtil.java    From ECTester with 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 File: BigIntegerTarget.java    From AVM with MIT License 4 votes vote down vote up
@Callable
public static int bitCountBigInteger(BigInteger testValue) {
    return testValue.bitCount();
}
 
Example 17
Source File: FixedPointType.java    From web3sdk with 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 18
Source File: HammingDistanceUDF.java    From incubator-hivemall with Apache License 2.0 4 votes vote down vote up
public static int hammingDistance(final BigInteger a, final BigInteger b) {
    BigInteger xor = a.xor(b);
    return xor.bitCount();
}
 
Example 19
Source File: FixedPointType.java    From web3j with 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 20
Source File: FixedPointType.java    From client-sdk-java with 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;
}