- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to shift bits in a BigInteger
To shift bits in a BigInteger, use the shiftLeft() or shiftRight() method.
shiftLeft() method
The java.math.BigInteger.shiftLeft(int n) returns a BigInteger whose value is (this << n). The shift distance, n, may be negative, in which case this method performs a right shift. It computes floor(this * 2n).
Example
import java.math.*; public class Demo { public static void main(String[] args) { BigInteger one; one = new BigInteger("15"); one = one.shiftLeft(2); System.out.println("Result: " +one); } }
Output
Result: 60
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).
Example
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); } }
Output
Result: 3
- Related Articles
- Shift right in a BigInteger in Java
- Shift left in a BigInteger in Java
- Java Program to flip a bit in a BigInteger
- Multiply one BigInteger to another BigInteger in Java
- Java Program to create random BigInteger within a given range
- Java program to count total bits in a number
- Java Program to perform XOR operation on BigInteger
- Java Program to perform AND operation on BigInteger
- Java Program to implement NOT operation on BigInteger
- Java Program to implement OR operation on BigInteger
- Java Program to implement andNot operation on BigInteger
- Negate a BigInteger in Java
- Shift the bits of an integer to the left in Numpy
- Shift the bits of an integer to the right in Numpy
- Subtract one BigInteger from another BigInteger in Java

Advertisements