Java.math.BigInteger.getLowestSetBit() 方法



描述

java.math.BigInteger.getLowestSetBit() 返回此 BigInteger 中最右側(最低位)一位的索引(最右側一位右邊的零位數)。如果此 BigInteger 不包含任何一位,則返回 -1。它計算 (this == 0? -1 : log2(this & -this))。

宣告

以下是 java.math.BigInteger.getLowestSetBit() 方法的宣告。

public int getLowestSetBit()

引數

返回值

此方法返回此 BigInteger 中最右側一位的索引。

異常

示例

以下示例展示了 math.BigInteger.getLowestSetBit() 方法的使用方法。

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("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
java_math_biginteger.htm
廣告
© . All rights reserved.