BigInteger shiftRight() in Java

shiftRight(): This method is available in java.math.BigInteger class of Java.

Syntax:

BigInteger java.math.BigInteger.shiftRight(int n)

This method takes one argument of type int as its parameter. This method returns a BigInteger whose value is (this >> n). Sign extension is performed. The shift distance, n, may be negative, in which case this method performs a left shift.(Computes floor(this / 2n).)

Parameters: One parameter is required for this method.

n: shift distance, in bits.

Returns: this >> n.

For Example:

BigInteger bigInteger = new BigInteger("1234")

int n = 2

bigInteger.shiftRight(n) = > It returns 308.

Approach

Java

import java.math.BigInteger;

public class BigIntegershiftRight {
    public static void main(String[] args) {

        BigInteger bigInteger = new BigInteger("1234");

        int n = 2;
        System.out.println(bigInteger.shiftRight(n));
    }
}

Output:

308

No comments:

Post a Comment