Java 程式以在 BigInteger 中移動位


若要在 BigInteger 中移動位,可使用 shiftLeft() 或 shiftRight() 方法。

shiftLeft() 方法

java.math.BigInteger.shiftLeft(int n) 返回一個值為 (this << n) 的 BigInteger。位移距離 n 可能為負,在這種情況下,此方法執行右移。它計算 floor(this * 2n)。

示例

 即時演示

import java.math.*;
public class Demo {
   public static void main(String[] args) {
      BigInteger one;
      one = new BigInteger("15");
      one = one.shiftLeft(2);
      System.out.println("Result: " +one);
   }
}

輸出

Result: 60

shiftRight() 方法

java.math.BigInteger.shiftRight(int n) 返回一個值為 (this >> n) 的 BigInteger。執行有符號擴充套件。位移距離 n 可能為負,在這種情況下,此方法執行左移。它計算 floor(this / 2n)。

示例

 即時演示

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

輸出

Result: 3

更新於:25-6 月-2020

87 次瀏覽

啟動你的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.