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