Java BigInteger getLowestSetBit() 方法

2025 年 3 月 20 日 | 阅读 2 分钟

Java BigInteger 类的 getLowestSetBit() 方法用于获取该 BigInteger 最右边的置位(set bit)的位置。该方法返回一个整数,其值为最右边置位右侧的重置位(0)的总数。该方法计算 (this == 0 ? -1 : log2(this & -this))。

语法

参数

不适用。

返回值

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

Exception

不适用

注意:如果此 BigInteger 不包含任何 1,则此方法返回 -1。

示例 1

输出

Index of the rightmost set bit in 8 is 3
Index of the rightmost set bit in 1 is 0
Index of the rightmost set bit in 10 is 1

示例 2

输出

Index of the rightmost set bit in 0 is -1
Index of the rightmost set bit in 15 is 0
 
下一主题Java BigInteger