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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP