Java BigInteger shiftRight()方法

Java BigInteger shiftRight()方法

java.math.BigInteger.shiftRight(int n) 方法返回一个BigInteger,其值为(this >> n)。移位距离n可能是负数,在这种情况下,该方法将执行左移。shiftRight()方法将把一个数字的二进制表示法中的每个数字向右移动n倍,移动方向的最后一位被替换为0。

语法

public BigInteger shiftRight(int n)

参数: 该方法需要一个整数类型的参数n,指的是以比特为单位的移动距离。

返回值: 该方法在将比特向右移位n次后返回BigInteger。

异常: 如果移位距离是Integer.MIN_VALUE,该方法可能会抛出一个ArithmeticException。

例子

输入 : BigInteger = 2300, n = 3
输出 : 287
解释:
2300=100011111100的二进制表示法
移位距离,n=3。
将100011111100右移3次后。
二进制表示成为100011111
而100011111的十进制相当于287。

输入 : BigInteger = 35000, n = 5
输出 : 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

**参考资料: **https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#shiftRight(int)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程