Wednesday, October 1, 2025
HomeLanguagesJavaBigInteger shiftRight() Method in Java

BigInteger shiftRight() Method in Java

prerequisite : BigInteger Basics

The java.math.BigInteger.shiftRight(int n) method returns a BigInteger whose value is (this >> n). The shift distance, n, may be negative, in which case this method performs a left shift. The shiftRight() method will move each digit in a number’s binary representation right by n times and the last bit in the direction of the shift is replaced by 0. This shiftRight() method Computes floor(this / 2^n).

Syntax:

public BigInteger shiftRight(int n)

Parameter: The method takes one parameter n of integer type which refers to the shift distance in bits.

Return Value: The method returns the BigInteger after shifting the bits to right by n times.

Exceptions: The method might throws an ArithmeticException if the shift distance is an Integer.MIN_VALUE.

Examples:

Input: BigInteger = 2300, n = 3
Output: 287
Explanation:
Binary Representation of 2300 = 100011111100
Shift distance, n = 3. 
After shifting 100011111100 right 3 times,
Binary Representation becomes 100011111
and Decimal equivalent of 100011111 is 287.

Input: BigInteger = 35000, n = 5
Output: 1093

Below program illustrates shiftRight(index) method of BigInteger:




// Program to demonstrate shiftRight()
// method of BigInteger 
  
import java.math.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create BigInteger object
        BigInteger biginteger = new BigInteger("2300");
  
        // Create a int i for Shift Distance
        int i = 3;
  
        // Call shiftRight() method on bigInteger at index i
        // store the return value as BigInteger
        BigInteger changedvalue = biginteger.shiftRight(i);
  
        String result = "After applying shiftRight by Shift Distance " + i + 
        " on " + biginteger + " New Value is " + changedvalue;
  
        // Print result
        System.out.println(result);
    }
}


Output:

After applying shiftRight by Shift Distance 3 on 2300 New Value is 287

Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#shiftRight(int)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32330 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11867 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11926 POSTS0 COMMENTS
Shaida Kate Naidoo
6815 POSTS0 COMMENTS
Ted Musemwa
7078 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6774 POSTS0 COMMENTS