Java-math-biginteger-shiftright
提供:Dev Guides
Java.math.BigInteger.shiftRight()メソッド
説明
- java.math.BigInteger.shiftRight(int n)*は、値が(this >> n)であるBigIntegerを返します。 符号拡張が実行されます。 シフト距離nは負の場合があります。この場合、このメソッドは左シフトを実行します。 floor(this/2 ^ n ^)を計算します。
宣言
以下は* java.math.BigInteger.shiftRight()*メソッドの宣言です。
public BigInteger shiftRight(int n)
パラメーター
*n* -ビット単位のシフト距離。
戻り値
このメソッドは、値がthis >> nであるBigIntegerオブジェクトを返します。
例外
*ArithmeticException* -シフト距離がInteger.MIN_VALUEの場合。
例
次の例は、math.BigInteger.shiftRight()メソッドの使用方法を示しています。
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("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