Java-math-biginteger-shiftleft

提供:Dev Guides
移動先:案内検索

Java.math.BigInteger.shiftLeft()メソッド

説明

  • java.math.BigInteger.shiftLeft(int n)*は、値が(this << n)であるBigIntegerを返します。 シフト距離nは負の場合があります。その場合、このメソッドは右シフトを実行します。 floor(this * 2 ^ n ^)を計算します。

宣言

以下は* java.math.BigInteger.shiftLeft()*メソッドの宣言です。

public BigInteger shiftLeft(int n)

パラメーター

*n* -ビット単位のシフト距離。

戻り値

このメソッドは、値がこの<< nであるBigIntegerオブジェクトを返します。

例外

*ArithmeticException* -シフト距離がInteger.MIN_VALUEの場合。

次の例は、math.BigInteger.shiftLeft()メソッドの使用法を示しています。

package com.finddevguides;

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