Java程式在BigInteger上實現andNot運算


TheBigInteger.andNot(BigInteger val)返回一個BigInteger,其值為(this & ~val)。此方法等效於and(val.not()),作為掩碼操作的簡便方法而提供。僅當this為負而val為正時,此方法才返回一個負的BigInteger。此處,“val”是要取反並與this BigInteger進行AND運算的值。

以下是示例−

示例

 即時演示

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

輸出

Result (andNot operation): 8

讓我們看另一個示例 -

示例

 即時演示

import java.math.*;
public class Demo {
   public static void main(String[] args) {
      BigInteger bi1, bi2, bi3;
      bi1 = new BigInteger("9");
      bi2 = new BigInteger("2");
      bi3 = bi1.andNot(bi2);
      String str = "Result of andNot operation is " +bi3;;
      System.out.println( str );
   }
}

輸出

Result of andNot operation is 9

更新日期: 29-Jun-2020

91次瀏覽

開啟你的職業生涯

完成課程並獲得認證

開始吧
廣告