Java.math.BigInteger.shiftLeft() 方法



描述

java.math.BigInteger.shiftLeft(int n) 返回一個 BigInteger,其值為 (this << n)。移位距離 n 可能為負數,在這種情況下,此方法執行右移。它計算 floor(this * 2n)。

宣告

以下是 java.math.BigInteger.shiftLeft() 方法的宣告。

public BigInteger shiftLeft(int n)

引數

n − 移位距離(以位為單位)。

返回值

此方法返回一個 BigInteger 物件,其值為 this << n。

異常

ArithmeticException − 如果移位距離為 Integer.MIN_VALUE。

示例

以下示例展示了 math.BigInteger.shiftLeft() 方法的用法。

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("10");

      // perform leftshift operation on bi1 using 2 and -2
      bi2 = bi1.shiftLeft(2);
      bi3 = bi1.shiftLeft(-2);

      String str1 = "Leftshift on " + bi1 + ", 2 times gives " +bi2;
      String str2 = "Leftshift on " + bi1 + ",-2 times gives " +bi3;

      // print bi2, bi3 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

讓我們編譯並執行以上程式,它將產生以下結果 −

Leftshift on 10, 2 times gives 40
Leftshift on 10,-2 times gives 2
java_math_biginteger.htm
廣告
© . All rights reserved.