📜  Java中的 BigInteger clearBit() 方法

📅  最后修改于: 2022-05-13 01:55:02.887000             🧑  作者: Mango

Java中的 BigInteger clearBit() 方法

先决条件: BigInteger 基础知识
clearBit()方法返回一个 BigInteger,用于清除 BigInteger 中的特定位位置。 BigInteger 二进制表示的索引 n 处的位将被清除意味着转换为零。在数学上我们可以说它是用来计算& ~(1<
句法:

public BigInteger clearBit(int n)

参数:该方法采用一个参数n,该参数是指需要清除的位的索引。
返回值:该方法在清除位位置n后返回BigInteger。
抛出:当 n 为负时,该方法可能会抛出ArithmeticException
例子:

Input: value = 2300, index = 3
Output: 2292
Explanation:
Binary Representation of 2300 = 100011111100
bit at index 3 is 1 so clear the bit at index 3 
Now Binary Representation becomes 100011110100
and Decimal equivalent of 100011110100 is 2292

Input: value = 5482549, index = 0
Output: 5482548

下面的程序说明了 BigInteger() 的 clearBit(index) 方法。

Java
/*
*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: Java/math/BigInteger.html#clearBit(int)