Java BigInteger setBit() 方法

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

Java BigInteger 类的 setBit() 方法用于设置位。此方法返回一个 BigInteger,其值等同于此 BigInteger 的指定位已设置。

语法

参数

n - 用户输入的要设置的位的索引。

返回值

此方法返回 ( this | (1<<n)).

Exception

ArithmeticException - n 为负数。

示例 1

输出

SetBit operation on 5 at index 1 gives 7

示例 2

输出

java.lang.ArithmeticException: Negative bit address

示例 3

输出

SetBit operation on 5 at index 2 gives 5
 
下一主题Java BigInteger