Java BigInteger clearBit()方法
clearBit() 方法返回一个BigInteger,用于清除BigInteger中的一个特定位。BigInteger的二进制表示法中位于索引n的位将被清除,即转换为零。从数学上讲,我们可以说它是用来计算 this &(1<<n)
。
语法:
public BigInteger clearBit(int n)
参数: 该方法需要一个参数n,指的是需要清除的位的索引。
返回值: 该方法在清除位的位置n后返回BigInteger。
抛出: 当n为负数时,该方法可能抛出一个ArithmeticException
。
示例:
输入: value = 2300, index = 3
输出: 2292
解释:
2300的二进制表示=100011111100
索引3的位是1,所以清除索引3的位
现在二进制表示成为100011110100
而100011110100的十进制相当于2292
输入: value = 5482549, index = 0
输出: 5482548
下面的程序说明了BigInteger()的clearBit(index)方法。
/*
*Program Demonstrate clearBit() method of BigInteger
*/
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating BigInteger object
BigInteger biginteger = new BigInteger("5482549");
// Creating an int i for index
int i = 0;
// Call clearBit() method on bigInteger at index i
// store the return BigInteger
BigInteger changedvalue = biginteger.clearBit(i);
String result = "After applying clearbit at index " +
i + " of " + biginteger+" New Value is " + changedvalue;
// Print result
System.out.println(result);
}
}
输出
After applying clearbit at index 0 of 5482549 New Value is 5482548
**参考资料: **https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#clearBit(int)