Java程序来移位BigInteger中的位
要移位BigInteger中的位,请使用shiftLeft()或shiftRight()方法。
shiftLeft()方法
java.math.BigInteger.shiftLeft(intn)返回一个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
shiftRight()方法
java.math.BigInteger.shiftRight(intn)返回一个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