- Java.math 包额外内容
- Java.math - 枚举
- Java.math - 讨论
Java.math.BigInteger.shiftRight() 方法
说明
java.math.BigInteger.shiftRight(int n) 返回其值为 (this >>= n) 的 BigInteger。将执行符号扩展。移位距离 n 可能为负,这种情况下,该方法将执行左移。它计算 floor(this / 2n)。
声明
以下是 java.math.BigInteger.shiftRight() 方法的声明。
public BigInteger shiftRight(int n)
参数
n - 以位为单位的移位距离。
返回值
此方法返回一个 BigInteger 对象,其值为 this >>= n。
异常
ArithmeticException - 如果移位距离为 Integer.MIN_VALUE。
示例
以下示例演示了 math.BigInteger.shiftRight() 方法的使用。
package com.tutorialspoint;
import java.math.*;
public class BigIntegerDemo {
public static void main(String[] args) {
// create 3 BigInteger objects
BigInteger bi1, bi2, bi3;
bi1 = new BigInteger("4");
// perform right shift operation on bi1 using 2 and -2
bi2 = bi1.shiftRight(2);
bi3 = bi1.shiftRight(-2);
String str1 = "Right shift on " +bi1+ ", 2 times gives " +bi2;
String str2 = "Right shift on " +bi1+ ", -2 times gives " +bi3;
// print bi2, bi3 values
System.out.println( str1 );
System.out.println( str2 );
}
}
让我们编译并运行以上程序,这将生成以下结果 -
Right shift on 4, 2 times gives 1 Right shift on 4, -2 times gives 16
java_math_biginteger.htm
广告