java.math.BigInteger.shiftRight(int n)方法返回一个BigInteger,其值为(this >> n)。移位距离n可以为负,在这种情况下,此方法执行左移。 shiftRight()方法会将数字的二进制表示形式中的每个数字右移n次,并且将移位方向的最后一位替换为0。此shiftRight()方法计算底数(this /2^n)。
用法:
public BigInteger shiftRight(int n)
参数:该方法采用整数类型的一个参数n,该参数n以位为单位。
返回值:该方法将位右移n次后返回BigInteger。
异常:如果移位距离为Integer.MIN_VALUE,则该方法可能会引发ArithmeticException。
例子:
Input: BigInteger = 2300, n = 3
Output: 287
Explanation:
Binary Representation of 2300 = 100011111100
Shift distance, n = 3.
After shifting 100011111100 right 3 times,
Binary Representation becomes 100011111
and Decimal equivalent of 100011111 is 287.
Input: BigInteger = 35000, n = 5
Output: 1093
以下示例程序旨在说明BigInteger的shiftRight(index)方法:
// Program to demonstrate shiftRight()
// method of BigInteger
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Create BigInteger object
BigInteger biginteger = new BigInteger("2300");
// Create a int i for Shift Distance
int i = 3;
// Call shiftRight() method on bigInteger at index i
// store the return value as BigInteger
BigInteger changedvalue = biginteger.shiftRight(i);
String result = "After applying shiftRight by Shift Distance " + i +
" on " + biginteger + " New Value is " + changedvalue;
// Print result
System.out.println(result);
}
}
输出:
After applying shiftRight by Shift Distance 3 on 2300 New Value is 287