要移位BigInteger中的位,请使用shiftLeft()或shiftRight()方法。
java.math.BigInteger.shiftLeft(int n)返回一个BigInteger,其值为(this << n)。移位距离n可以为负,在这种情况下,此方法执行右移。它计算地板(此* 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
java.math.BigInteger.shiftRight(int n)返回一个BigInteger,其值为(this >> n)。进行符号扩展。移位距离n可以为负,在这种情况下,此方法执行左移。它计算地板(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