BigInteger bit shift left and right

In this chapter you will learn:

  1. How to left shift bit value on BigInteger
  2. How to right shift bit on BigInteger

Left shift bit value on BigInteger

BigInteger shiftLeft(int n) Returns a BigInteger whose value is (this << n).

import java.math.BigInteger;
/*  j a  va 2s.c  om*/
public class Main {
  public static void main(String[] argv) throws Exception {
    byte[] bytes = new byte[] { 0x1, 0x00, 0x00 }; 
    BigInteger bi = new BigInteger(bytes);
    bi = bi.shiftLeft(3);
    System.out.println(bi);
  }
}

The output:

Right shift bit on BigInteger

BigInteger shiftRight(int n) returns a BigInteger whose value is (this >> n).

import java.math.BigInteger;
//  j a  va 2s.co  m
public class Main {
  public static void main(String[] argv) throws Exception {
    byte[] bytes = new byte[] { 0x1, 0x00, 0x00 }; 
    BigInteger bi = new BigInteger(bytes);
    bi = bi.shiftRight(1);
    System.out.println(bi);
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to know if a BigInteger value prime
  2. How to generate a prime number from BigInteger
Home » Java Tutorial » BigDecimal BigInteger

BigDecimal

    BigDecimal
    BigDecimal constants
    BigDecimal Rounding mode
    BigDecimal creation
    BigDecimal calculation
    BigDecimal convert
    BigDecimal Comparison
    BigDecimal to String
    BigDecimal decimal point
    BigDecimal precision
    BigDecimal format

BigInteger

    BigInteger class
    BigInteger creation
    BigInteger add, subtract, multiply and divide
    BigInteger power and modPow
    BigInteger conversion
    BigInteger to String
    BigInteger bit and,or,xor,test,flip,negate
    BigInteger bit shift left and right
    BigInteger prime value
    BigDecimal
    BigDecimal constants
    BigDecimal Rounding mode
    BigDecimal creation
    BigDecimal calculation
    BigDecimal convert
    BigDecimal Comparison
    BigDecimal to String
    BigDecimal decimal point
    BigDecimal precision
    BigDecimal format