BigInteger flipBit(int n)

优质
小牛编辑
123浏览
2023-12-01

描述 (Description)

java.math.BigInteger.flipBit(int n)返回一个BigInteger,其值等于此BigInteger,并且指定的位被翻转。 它计算(这^(1“”n))。

声明 (Declaration)

以下是java.math.BigInteger.flipBit()方法的声明。

public BigInteger flipBit(int n)

参数 (Parameters)

n - 要翻转的位索引。

返回值 (Return Value)

此方法返回一个BigInteger对象,其值为^(1 << n)。

异常 (Exception)

ArithmeticException - n为负数。

例子 (Example)

以下示例显示了math.BigInteger.flipBit()方法的用法。

package cn.xnip;
import java.math.*;
public class BigIntegerDemo {
   public static void main(String[] args) {
      // create 2 BigInteger objects
      BigInteger bi1, bi2;
      // assign value to bi1
      bi1 = new BigInteger("8");//1000
      // perform flipbit operation on bi1 with index 1
      bi2 = bi1.flipBit(1);
      String str = "Flipbit operation on " +bi1+ " at index 1 gives " +bi2;
      // print bi2 value
      System.out.println( str );
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

Flipbit operation on 8 at index 1 gives 10