Java中的BigInteger bitCount()方法

先决条件: 大整数基础 这个 JAVA数学大整数。比特数() 方法返回此BigInteger的两个补码表示中与其符号位不同的位数。当在大整数上实现位向量样式集时,此方法非常有用。 语法:

null
public int bitCount()

参数: 该方法不采用任何参数。 返回值: 该方法用于返回此BigInteger的两个补码表示中与其符号位不同的位数。 例如:

Input: value = 2300 Output: 7Explanation:Binary signed 2's complement of 2300 = 0000100011111100Singned bit is 0 because 2300 is positiveso no of 1 in 0000100011111100 is bitCountSo bitCount in 0000100011111100 = 7Input: value = 5482549Output: 11

下面的程序演示BigInteger的bitCount()方法。

JAVA

/*
*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()

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享