- Java.math package classes
- Java.math - Home
- Java.math - BigDecimal
- Java.math - BigInteger
- Java.math - MathContext
- Java.math package extras
- Java.math - Enumerations
- Java.math - Discussion
Java.math.BigInteger.bitCount() Method
Description
The java.math.BigInteger.bitCount() returns the number of bits in the two's complement representation of this BigInteger that differ from its sign bit. This method is useful when implementing bit-vector style sets atop BigIntegers.
Declaration
Following is the declaration for java.math.BigInteger.bitCount() method.
public int bitCount()
Parameters
NA
Return Value
This method returns number of bits in the two's complement representation of this BigInteger that differ from its sign bit.
Exception
NA
Example
The following example shows the usage of math.BigInteger.bitCount() method.
package com.tutorialspoint; import java.math.*; public class BigIntegerDemo { public static void main(String[] args) { // create 2 BigInteger objects BigInteger bi1, bi2; // create 2 int objects int i1, i2; // assign values to bi1, bi2 bi1 = new BigInteger("7"); bi2 = new BigInteger("-7"); // perform bitcount operation on bi1, bi2 i1 = bi1.bitCount(); i2 = bi2.bitCount(); String str1 = "Result of bitcount operation on " + bi1 +" is " +i1; String str2 = "Result of bitcount operation on " + bi2 +" is " +i2; // print i1, i2 values System.out.println( str1 ); System.out.println( str2 ); } }
Let us compile and run the above program, this will produce the following result −
Result of bitcount operation on 7 is 3 Result of bitcount operation on -7 is 2
java_math_biginteger.htm
Advertisements