Java 程式以在 BigInteger 上實現 NOT 運算


BigInteger.not() 方法返回一個 BigInteger,其值為 (~this)。當且僅當此 BigInteger 非負時,此方法才返回負值。

以下是示例 -

示例

 即時演示

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

輸出

Result (not operation): -7

讓我們看另一個示例 -

示例

 即時演示

import java.math.*;
public class Demo {
   public static void main(String[] args) {
      BigInteger bi1, bi2, bi3, bi4;
      bi1 = new BigInteger("9");
      bi2 = new BigInteger("-12");
      bi3 = bi1.not();
      bi4 = bi2.not();
      String str1 = "Result of not operation on " + bi1 +" gives " +bi3;
      String str2 = "Result of not operation on " + bi2 +" gives " +bi4;
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

輸出

Result of not operation on 9 gives -10
Result of not operation on -12 gives 11

更新時間: 29-Jun-2020

101 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.