# Java Integer rotateLeft() Method

Java `rotateLeft()` method is a part of the `Integer` class of the `java.lang` package. This method is used to return the value obtained by rotating two's complement of the binary equivalent of the number passed as an argument towards the left (i.e towards higher-order) by the distance specified in the argument.

This method is based on the bit shifting operation where all the bits of the binary number are shifted left or right to a definite number of places. For example, `rotateLeft(10, 1)` refers to the rotation of binary equivalent of 10 towards left by one bit. The equivalent binary of 10 is 00001010, and by shifting 1 bit left it will be converted into 00010100 (i.e is equal to 20).

### Syntax:

``public static int rotateLeft(int i, int distance) ``

### Parameters:

The parameters passed are integer value who's binary equivalent bits are to be rotated and int distance to specify the number of positions the bits are to be rotated.

### Returns:

Returns the value obtained by rotating two's complement of the binary equivalent of the number passed as a parameter towards the left (i.e towards higher-order) by the distance passed in the method.

## Example 1:

Here, a positive and a negative number is taken for a better understanding of the method. The `Integer.toBinaryString()` method is used for representing the number in its binary equivalent.

``````import java.lang.*;

public class StudyTonight
{
public static void main(String[] args)
{
int n1 = 4;
int n2 = -4;
int val = 3;
System.out.println("Binary equivalent is : " + Integer.toBinaryString(n1));
for(int i = 0; i < val; i++)
{
n1 = Integer.rotateLeft(n1, val); //returns the value after rotation
System.out.println(n1);
}
System.out.println("Binary equivalent is : " + Integer.toBinaryString(n2));
for(int i = 0; i < val; i++)
{
n2 = Integer.rotateLeft(n2, val); //returns the value after rotation
System.out.println(n2);
}
}
} ``````

Binary equivalent is : 100
32
256
2048
Binary equivalent is : 11111111111111111111111111111100
-25
-193
-1537

## Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

``````import java.lang.*;
import java.util.*;

public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.println("Enter the value and distance");
Scanner sc = new Scanner (System.in);
int n = sc.nextInt();
int val = sc.nextInt();
System.out.println("Binary equivalent is : " + Integer.toBinaryString(n));
for(int i = 0; i < val; i++)
{
n = Integer.rotateLeft(n, val); //returns the value after rotation
System.out.println(n);
}
}
catch(Exception e)
{
System.out.println("Invalid Input");
}
}
} ``````

Enter the value and distance
5 4
Binary equivalent is : 101
80
1280
20480
327680
*****************************
Enter the value and distance
-30 4
Binary equivalent is : 11111111111111111111111111100010
-465
-7425
-118785
-1900545
********************************************
Enter the value and distance
0x567 9
Invalid Input

## Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.