當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.math.BigInteger.shiftRight()用法及代碼示例


描述

這個java.math.BigInteger.shiftRight(int n)返回一個 BigInteger,其值為 (this >> n)。執行符號擴展。移位距離 n 可能為負,在這種情況下,此方法執行左移。它計算樓層(這個 /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.shiftRight() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。