Java BigInteger bitCount()方法
java.math.BigInteger.bitCount() 方法返回此BigInteger的二补表示中与符号位不同的位数。这个方法在实现BigInteger上面的位向量风格的集合时非常有用。
语法:
public int bitCount()
参数: 该方法不接受任何参数。
返回值: 该方法用于返回该BigInteger的二补表示中与符号位不同的位数。
示例:
输入: value = 2300
输出: 7
解释:
2300的二进制有符号2的补码=0000100011111100
带符号的位是0,因为2300是正数
所以0000100011111100中的1的数量是bitCount
所以0000100011111100中的bitCount=7
输入: value = 5482549
输出: 11
下面的程序说明了BigInteger的bitCount()方法。
/*
*Program Demonstrate bitCount() method of BigInteger
*/
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creates BigInteger objects
BigInteger biginteger = new BigInteger("2300");
// Calling bitCount() method on bigInteger
int count = biginteger.bitCount();
String result = "BitCount of " + biginteger + " is " + count;
// Print result
System.out.println(result);
}
}
输出
BitCount of 2300 is 7
参考资料 : https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#bitCount()