Shift right in a BigInteger in Java

To shift right in a BigInteger, use the shiftRight() method.

The java.math.BigInteger.shiftRight(int n) 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. It computes floor(this / 2n).

The following is an example −


 Live Demo

import java.math.*;
public class Demo {
   public static void main(String[] args) {
      BigInteger one;
      one = new BigInteger("25");
      one = one.shiftRight(3);
      System.out.println("Result: " +one);


Result: 3

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 29-Jun-2020


Kickstart Your Career

Get certified by completing the course

Get Started