- Java.math包附加資訊
- Java.math - 列舉
- Java.math - 討論
Java.math.BigInteger.bitCount()方法
說明
java.math.BigInteger.bitCount()返回此BigInteger的二進位制補碼錶示中與符號位不同的位數。實現基於BigInteger的位向量樣式集合時,此方法非常有用。
宣告
以下是java.math.BigInteger.bitCount()方法的宣告。
public int bitCount()
引數
不適用
返回值
此方法返回此BigInteger的二進位制補碼錶示中與符號位不同的位數。
異常
不適用
示例
下面的示例展示了math.BigInteger.bitCount()方法的用法。
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 );
}
}
讓我們編譯並執行上面的程式,這將產生以下結果-。
Result of bitcount operation on 7 is 3 Result of bitcount operation on -7 is 2
java_math_biginteger.htm
廣告