Java 程式對 BigInteger 執行 XOR 操作


java.math.BigInteger.xor(BigInteger val) 返回一個 BigInteger,其值為 (this ^ val)。此方法僅當 this 和 val 中恰好有一個為負值時,才返回一個負 BigInteger。此處,val 是要與該 BigInteger 進行 XOR 操作的值。

首先,建立兩個 BigInteger 物件 −

one = new BigInteger("6");
two = new BigInteger("-5");

現在,執行 XOR −

three = one.xor(two);

下面是一個示例 −

示例

 線上演示

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

輸出

Result: -3

更新於: 2020 年 6 月 29 日

158 次瀏覽

啟動您的 職業

完成課程以獲得認證

開始
廣告
© . All rights reserved.