The java.math.BigInteger.shiftLeft(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 right shift.shiftLeft() method will moves each digit in a number’s binary representation left by n times and the last bit in the direction of the shift is replaced by 0. This ShiftLeft() method Computes floor(this * 2^n).
public BigInteger shiftLeft(int n)
Parameters: 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 left by n times.
Exceptions: The method might throw an ArithmeticException if the shift distance is Integer.MIN_VALUE.
Input: value = 2300, shift distance = 3 Output: 18400 Explanation: Binary Representation of 2300 = 100011111100 shift distance = 3 after shifting 100011111100 left 3 times then Binary Representation becomes 100011111100000 and Decimal equivalent of 100011111100000 is 18400. Another way of expressing the same can be 2300*(2^3)=18400 Input: value = 35000, index = 5 Output: 1120000
Below program illustrate shiftLeft(index) method of BigInteger.
After applying ShiftLeft by Shift Distance 3 on 2300 New Value is 18400
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.