Integer rotateLeft() Method in Java
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).
Syntax:
public static int rotateLeft(int value, int shifts)
Parameters: The method takes two parameters:
- a : This is of intger 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.
Examples:
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.
// Java program to illustrate the // Java.lang.Integer.rotateLeft() method import java.lang.*; public class Geeks { public static void main(String[] args) { int a = 2; int shifts = 0; while (shifts < 6) // It will return the value obtained by rotating left { a = Integer.rotateLeft(a, 2); System.out.println(a); shifts++; } } } |
8 32 128 512 2048 8192
Program 2: For a negative number.
// Java program to illustrate the // Java.lang.Integer.rotateLeft() method import java.lang.*; public class Geeks { public static void main(String[] args) { int a = -16; int shifts = 0; while (shifts < 2) // It will return the value obtained by rotating left { a = Integer.rotateLeft(a, shifts); System.out.println(a); shifts++; } } } |
-16 -31
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
// Java program to illustrate the // Java.lang.Integer.rotateLeft() method import java.lang.*; public class Geeks { public static void main(String[] args) { int a = 15.71; int shifts = 0; while (shifts < 2) { a = Integer.rotateLeft(a, shifts); System.out.println(a); shifts++; } int b = "61"; int shifts2 = 0; while (shifts2 < 2) { b = Integer.rotateLeft(b, shifts2); System.out.println(b); shifts2++; } } } |
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
Recommended Posts:
- Integer sum() Method in Java
- Java Integer bitCount() method
- Integer reverse() Method In Java
- Integer intValue() Method in Java
- Integer valueOf() Method in Java
- Java Integer compareUnsigned() method
- Integer.numberOfTrailingZeros() Method in Java with Example
- Java Integer compare() method
- Integer decode() Method in Java
- Integer hashCode() Method in Java
- Java Integer byteValue() method
- Java Integer compareTo() method
- Integer reverseBytes() Method in Java
- Integer highestOneBit() Method in Java
- Integer toOctalString() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.



