int getLowestSetBit()

优质
小牛编辑
130浏览
2023-12-01

描述 (Description)

java.math.BigInteger.getLowestSetBit()返回此BigInteger中最右边(最低位)一位的索引(最右边一位右边的零位数)。 如果此BigInteger不包含任何一个位,则返回-1。 它计算(这= = 0?-1:log2(this&-this))。

声明 (Declaration)

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

public int getLowestSetBit()

参数 (Parameters)

NA

返回值 (Return Value)

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

异常 (Exception)

NA

例子 (Example)

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

package cn.xnip;
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