Shift right in a BigInteger in Java

Java 8Object Oriented ProgrammingProgramming

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
Published on 20-Dec-2018 10:50:44