Example usage for java.math BigInteger bitCount

List of usage examples for java.math BigInteger bitCount

Introduction

In this page you can find the example usage for java.math BigInteger bitCount.

Prototype

public int bitCount() 

Source Link

Document

Returns the number of bits in the two's complement representation of this BigInteger that differ from its sign bit.

Usage

From source file:Main.java

public static void main(String[] args) {

    BigInteger bi1 = new BigInteger("7");
    BigInteger bi2 = new BigInteger("-7");

    // perform bitcount operation on bi1, bi2
    int i1 = bi1.bitCount();
    int i2 = bi2.bitCount();

    System.out.println(i1);/*from   www .  java 2 s.  c om*/
    System.out.println(i2);
}

From source file:Main.java

public static int getNafWeight(BigInteger k) {
    if (k.signum() == 0) {
        return 0;
    }//from   w ww  .jav  a2s  .  c o m

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

    return diff.bitCount();
}

From source file:org.esa.cci.sst.tool.Configuration.java

private long parsePattern(String key, String value) {
    final long pattern;
    try {/*from   www.j ava2s . com*/
        final BigInteger bigInteger = new BigInteger(value, 16);
        if (bigInteger.bitLength() > 64) {
            throw new ToolException("Too many bits in pattern: " + key, ToolException.TOOL_CONFIGURATION_ERROR);
        }
        if (bigInteger.bitCount() > 1) {
            throw new ToolException("Too many bits set in pattern: " + key,
                    ToolException.TOOL_CONFIGURATION_ERROR);
        }
        pattern = bigInteger.longValue();
    } catch (NumberFormatException e) {
        throw new ToolException("Cannot parse pattern: " + key, e, ToolException.TOOL_CONFIGURATION_ERROR);
    }
    return pattern;
}