📜  Java中的 BigInteger flipBit() 方法

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

Java中的 BigInteger flipBit() 方法

先决条件:BigInteger 基础知识
Java.math.BigInteger.flipBit(index)方法返回一个 BigInteger,用于翻转 BigInteger 中的特定位位置。此方法计算 (bigInteger ^ (1<句法:

public BigInteger flipBit(int index)

参数:该方法接受一个整数类型的参数索引,并引用要翻转的位的位置。
返回值:该方法在index位置翻转其位后返回 bigInteger。
抛出:当 index 的值为负时,该方法会抛出ArithmeticException
例子:

Input: value = 2300 , index = 1
Output: 2302
Explanation:
Binary Representation of 2300 = 100011111100
bit at index 1 is 0 so flip the bit at index 1 and it becomes 1. 
Now Binary Representation becomes 100011111110
and Decimal equivalent of 100011111110 is 2302

Input: value = 5482549 , index = 5
Output: 5482517

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

Java
/*
*Program Demonstrate flipBit() 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 = 5;
 
        // Call flipBit() method on bigInteger at index i
        // store the return BigInteger
        BigInteger changedvalue = biginteger.flipBit(i);
 
        String result = "After applying flipBit at index " + i +
        " of " + biginteger+ " New Value is " + changedvalue;
 
        // Print result
        System.out.println(result);
    }
}


输出:
After applying flipBit at index 5 of 5482549 New Value is 5482517

参考: https: Java/math/BigInteger.html#clearBit(int)