Java中的 BigInteger getLowestSetBit() 方法
先决条件: BigInteger 基础
Java.math.BigInteger.getLowestSetBit()方法返回此 BigInteger 的最右边(最低阶)设置位的索引。这意味着此方法返回最右边设置位右侧的零位或未设置位的数量。如果 BigInteger 不包含设置位,则此方法将返回 -1。该方法计算(thisBigInteger==0? -1 : log2(thisBigInteger & -thisBigInteger)) 。
句法:
public int getLowestSetBit()
参数:该方法不接受任何参数。
返回值:该方法返回此 BigInteger 中最右边设置位的索引。
例子:
Input: value = 2300
Output: 2
Explanation:
Binary Representation of 2300 = 100011111100
The lowest set bit index is 2
Input: value = 35000
Output: 3
下面的程序说明了 BigInteger 的 getLowestSetBit() 方法:
// Program to illustrate the getLowestSetBit()
// method of BigInteger
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Create BigInteger object
BigInteger biginteger = new BigInteger("2300");
// Call getLowestSetBit() method on bigInteger
// Store the return value as Integer lowestsetbit
int lowestSetbit = biginteger.getLowestSetBit();
String lsb = "After applying getLowestSetBit on " + biginteger +
" we get index of lowest set bit = " + lowestSetbit;
// Printing result
System.out.println(lsb);
}
}
输出:
After applying getLowestSetBit on 2300 we get index of lowest set bit = 2
参考: https: Java/math/BigInteger.html#getLowestSetBit()