位置:首頁 > Java技術 > Java.math包 > Java.math.BigInteger.getLowestSetBit()方法實例

Java.math.BigInteger.getLowestSetBit()方法實例

 java.math.BigInteger.getLowestSetBit()返回BigInteger最右邊的(最低階)的一個1比特位(零比特到最右邊的1位的權數)的索引。如果此BigInteger包含無比特位,它返回-1。它計算(this==0? -1 : log2(this & -this)).

聲明

以下是java.math.BigInteger.getLowestSetBit()方法的聲明

public int getLowestSetBit()

參數

  • NA

返回值

此方法返回BigInteger最右邊的1位的索引。

異常

  • NA

例子

下麵的例子顯示math.BigInteger.getLowestSetBit()方法的用法

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("8");//1000
	bi2 = new BigInteger("7");//0111

	// perform getLowestSetBit on bi1, bi2
	i1 = bi1.getLowestSetBit();
	i2 = bi2.getLowestSetBit();

	String str1 = "Index of rightmost one bit in " +bi1+ " is " +i1;
	String str2 = "Index of rightmost one bit in " +bi2+ " is " +i2;

	// print i1, i2 values
	System.out.println( str1 );
	System.out.println( str2 );
    }
}

讓我們編譯和運行上麵的程序,這將產生以下結果:

Index of rightmost one bit in 8 is 3
Index of rightmost one bit in 7 is 0