Java BigInteger testBit() 方法

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

Java BigInteger 类的 testBit() 方法用于检查指定的位是否已设置。如果指定的位已设置,则此方法返回 true,否则返回 false。此方法计算 (this & (1<<n)) != 0)。

语法

参数

n - 要测试的位的索引

返回值

如果指定的位已设置,则该方法返回 true,否则返回 false。

Exception

当 n 为负数时,此方法将抛出 ArithmeticException。

示例 1

输出

Test Bit on 5 at index 2 returns true
Test Bit on 5 at index 3 returns false

示例 2

输出

java.lang.ArithmeticException: Negative bit address
 
下一主题Java BigInteger