BigInteger shiftRight(int n)

优质
小牛编辑
125浏览
2023-12-01

描述 (Description)

java.math.BigInteger.shiftRight(int n)返回一个BigInteger,其值为(this“”n)。 执行签名扩展。 移位距离n可以是负的,在这种情况下,该方法执行左移。 它计算楼层(这/ 2 n )。

声明 (Declaration)

以下是java.math.BigInteger.shiftRight()方法的声明。

public BigInteger shiftRight(int n)

参数 (Parameters)

n - 移位距离,以位为单位。

返回值 (Return Value)

此方法返回一个BigInteger对象,其值为this >> n。

异常 (Exception)

ArithmeticException - 如果移位距离为Integer.MIN_VALUE。

例子 (Example)

以下示例显示了math.BigInteger.shiftRight()方法的用法。

package cn.xnip;
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