Java Data Type Tutorial - Java BigInteger.bitCount()








Syntax

BigInteger.bitCount() has the following syntax.

public int bitCount()

Example

In the following code shows how to use BigInteger.bitCount() method.

//from www  . j  ava 2s  . c  o  m

import java.math.BigInteger;

public class Main {

  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);
    System.out.println(i2);
  }
}

The code above generates the following result.