Bit shifting is a type of bitwise operation which is performed on all the bits of a binary value by moving the bits by a definite number of places towards left or right. Java has a single Logical left shift operator (<< ). When the value 0001(i.e., 1) is shifted left, it becomes 0010(i.e., 2) which on shifting again to the left becomes 0100 or 4. The same process is applied for the right shift operation.
The java.lang.Integer.rotateLeft() method is used to shift the bits of an Integer value, represented in its binary 2’s complementary form towards left by a specified number of bits. (Bits are shifted to the left or higher-order).
public static int rotateLeft(int value, int shifts)
Parameters: The method takes two parameters:
- a : This is of integer type and refers to the value on which operation is to be performed.
- shifts : This is also of integer type and refers to the distance of rotation.
Return Value: The method returns the value obtained by rotating the two’s complement binary representation of the specified int value towards left by the specified number of shift bits.
Input: 12 Output: 24 Explanation: Consider an integer a = 12 Binary Representation = 00001100 After 1 left shift it will become=00011000 So that is= 24
Below programs illustrate the java.lang.Integer.rotateLeft() method.
Program 1: For a positive number.
8 32 128 512 2048 8192
Program 2: For a negative number.
Program 3: For a decimal value and string.
Note: It returns an error message when a decimal value and string is passed as an argument
prog.java:9: error: incompatible types: possible lossy conversion from double to int int a = 15.71; ^ prog.java:18: error: incompatible types: String cannot be converted to int int b = "61"; ^ 2 errors
- Integer sum() Method in Java
- Java Integer compareTo() method
- Integer reverse() Method In Java
- Integer reverseBytes() Method in Java
- Integer toOctalString() Method in Java
- Integer valueOf() Method in Java
- Integer doubleValue() Method in Java
- Integer.numberOfLeadingZeros() Method in Java With Example
- Integer.numberOfTrailingZeros() Method in Java with Example
- Integer signum() Method in Java
- Integer shortValue() Method in Java
- Integer rotateRight() Method in Java
- Integer floatValue() Method in Java
- Java Integer bitCount() method
- Java Integer compare() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet