### Java Integer rotateLeft() Method

The `Integer.rotateLeft()` method in Java is used to rotate the bits of the specified integer value to the left by a given distance.

1. Introduction
2. `rotateLeft()` Method Syntax
3. Examples
• Rotating Bits of a Positive Integer
• Rotating Bits of a Negative Integer
• Rotating Bits with Distance Greater Than 32
4. Real-World Use Case
5. Conclusion

## Introduction

The `Integer.rotateLeft()` method is a static method in the `Integer` class in Java. It rotates the bits of the specified integer value to the left by the specified distance. The bits that are shifted out from the left end are reintroduced at the right end. This method is useful for various bit manipulation tasks, such as in cryptography, hashing algorithms, and computer graphics.

## rotateLeft()() Method Syntax

The syntax for the `Integer.rotateLeft()` method is as follows:

``````public static int rotateLeft(int i, int distance)
``````
• i: The integer value whose bits are to be rotated.
• distance: The number of positions to rotate the bits to the left.

The method returns:

• An integer whose bits are the result of rotating the bits of the specified integer to the left by the specified distance.

## Examples

### Rotating Bits of a Positive Integer

The `rotateLeft()` method can be used to rotate the bits of a positive integer to the left.

#### Example

``````public class RotateLeftExample {
public static void main(String[] args) {
int number = 13; // Binary representation: 00000000 00000000 00000000 00001101
int distance = 2;
int rotated = Integer.rotateLeft(number, distance);

System.out.println("Original binary: " + Integer.toBinaryString(number));
System.out.println("Rotated binary: " + Integer.toBinaryString(rotated));
System.out.println("Rotated integer: " + rotated);
}
}
``````

Output:

``````Original binary: 1101
Rotated binary: 110100
Rotated integer: 52
``````

In this example, the bits of the integer `13` are rotated to the left by `2` positions, resulting in the integer `52`.

### Rotating Bits of a Negative Integer

The `rotateLeft()` method can also be used to rotate the bits of a negative integer to the left.

#### Example

``````public class RotateLeftNegativeExample {
public static void main(String[] args) {
int number = -13; // Binary representation: 11111111 11111111 11111111 11110011
int distance = 3;
int rotated = Integer.rotateLeft(number, distance);

System.out.println("Original binary: " + Integer.toBinaryString(number));
System.out.println("Rotated binary: " + Integer.toBinaryString(rotated));
System.out.println("Rotated integer: " + rotated);
}
}
``````

Output:

``````Original binary: 11111111111111111111111111110011
Rotated binary: 11111111111111111111111110011111
Rotated integer: -97
``````

In this example, the bits of the integer `-13` are rotated to the left by `3` positions, resulting in the integer `-97`.

### Rotating Bits with Distance Greater Than 32

If the distance is greater than 32, the method effectively performs the rotation by `distance % 32` positions, as the integer is a 32-bit value.

#### Example

``````public class RotateLeftLargeDistanceExample {
public static void main(String[] args) {
int number = 13; // Binary representation: 00000000 00000000 00000000 00001101
int distance = 34; // Equivalent to rotating by 34 % 32 = 2 positions
int rotated = Integer.rotateLeft(number, distance);

System.out.println("Original binary: " + Integer.toBinaryString(number));
System.out.println("Rotated binary: " + Integer.toBinaryString(rotated));
System.out.println("Rotated integer: " + rotated);
}
}
``````

Output:

``````Original binary: 1101
Rotated binary: 110100
Rotated integer: 52
``````

In this example, the bits of the integer `13` are rotated to the left by `34` positions, which is equivalent to rotating by `2` positions, resulting in the integer `52`.

## Real-World Use Case

### Bitwise Operations in Cryptography

In a real-world application, you might use the `Integer.rotateLeft()` method to perform bitwise operations in cryptographic algorithms, such as rotating bits for data encryption or hashing.

#### Example

``````public class CryptographyExample {
public static void main(String[] args) {
int key = 0b10101010101010101010101010101010; // Example key
int rotatedKey = Integer.rotateLeft(key, 5);

System.out.println("Original key binary: " + Integer.toBinaryString(key));
System.out.println("Rotated key binary: " + Integer.toBinaryString(rotatedKey));
System.out.println("Rotated key integer: " + rotatedKey);
}
}
``````

Output:

``````Original key binary: 10101010101010101010101010101010
Rotated key binary: 1010101010101010101010101010101010
Rotated key integer: 1431655765
``````

In this example, the bits of the key are rotated to the left by `5` positions for use in a cryptographic algorithm, resulting in the integer `1431655765`.

## Conclusion

The `Integer.rotateLeft()` method in Java is a powerful and useful tool for rotating the bits of an integer to the left by a specified distance. By understanding how to use this method, you can efficiently handle tasks that involve bit manipulation in your Java applications. Whether you are dealing with positive or negative integers, or implementing cryptographic algorithms, the `rotateLeft()` method provides a reliable solution for these tasks.