Java 程式來實現 BigInteger 上的 OR 運算


TheBigInteger.or(BigInteger val) 返回一個 BigInteger,其值為 (this | val)。此方法當且僅當 this 或 val 為負數的情況下才返回一個負 BigInteger。

在此,“val”是要用此 BigInteger 進行 OR 運算的值。

下面是一個示例 −

示例

 即時演示

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

輸出

Result (or operation): 6

讓我們看另一個示例 −

示例

 即時演示

import java.math.*;
public class Demo {
   public static void main(String[] args) {
      BigInteger bi1, bi2, bi3;
      bi1 = new BigInteger("9");
      bi2 = new BigInteger("16");
      bi3 = bi1.or(bi2);
      String str = "OR operation on " + bi1 +" and " + bi2 + " gives " +bi3;
      System.out.println( str );
   }
}

輸出

OR operation on 9 and 16 gives 25

更新於: 29-Jun-2020

125 瀏覽

啟動你的 職業

透過完成課程獲得認證

入門
廣告
© . All rights reserved.