Java 程式,翻轉 BigInteger 中的一個二進位制位


要在 Java 中翻轉 BigInteger 中的一個二進位制位,可使用 flipBit() 方法。該方法返回一個 BigInteger,其值等同於此 BigInteger,但指定二進位制位已被翻轉。

示例

 演示

import java.math.*;
public class Demo {
   public static void main(String[] args) {
      BigInteger one, two;
      one = new BigInteger("7");
      one = one.flipBit(3);
      System.out.println("Result: " +one);
   }
}

輸出

Result: 15

讓我們看另一個示例。

示例

 演示

import java.math.*;
public class Demo {
   public static void main(String[] args) {
      BigInteger bi1, bi2;
      bi1 = new BigInteger("8");
      bi2 = bi1.flipBit(1);
      String str = "Flipbit operation on " +bi1+ " at index 1 gives " +bi2;
      System.out.println( str );
   }
}

輸出

Flipbit operation on 8 at index 1 gives 10

更新時間:25-Jun-2020

121 次瀏覽

開啟你的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.