java.math.BigInteger.bitLength() 返回在此BigInteger的最小的二進位制二補數表示的位元數,不包括符號位。為正BigIntegers,這相當於位元在普通二進位制表示的數目。它的計算方法為 (ceil(log2(this < 0 ? -this : this+1))).
以下是java.math.BigInteger.bitLength()方法的宣告
public int bitLength()
NA
此方法返回在此BigInteger的位元數的最小的二進位制二補數表示,不包括符號位。
NA
下面的例子顯示math.BigInteger.bitLength()方法的用法
package com.yiibai; 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 bitlength operation on bi1, bi2 i1 = bi1.bitLength(); i2 = bi2.bitLength(); String str1 = "Result of bitlength operation on " + bi1 +" is " +i1; String str2 = "Result of bitlength operation on " + bi2 +" is " +i2; // print i1, i2 values System.out.println( str1 ); System.out.println( str2 ); } }
讓我們編譯和執行上面的程式,這將產生以下結果:
Result of bitlength operation on 7 is 3 Result of bitlength operation on -7 is 3