java.lang.Integer rotateLeft(int i, int distance)

Description :

This java tutorial shows how to use the rotateLeft() method of Integer class under java.lang package. This method returns the value obtained by rotating the two’s complement binary representation of the specified int value left by the specified number of bits. (Bits shifted out of the left hand, or high-order, side reenter on the right, or low-order.)

Method Syntax :

public static int rotateLeft(int i, int distance)

Parameter Input :

DataType Parameter Description
int i the integer value that we want to rotate to the left
int distance this method argument specify the rotation distance

Method Returns :

The toOctalString(int i) method simply returns the octal string equivalent of int method parameter.

Compatibility Version :

Requires Java 1.5 and up

Exception :


Java Code Example :

This java example source code demonstrates the use of rotateLeft() method of Integer class. Basically we printed the equivalent binary string of the int argument and then we rotate to the left the two’s complement. Afterwards we convert the rotated value into binary string to see the impact of the rotation.


import static java.lang.System.*;

 * This example source code demonstrates 
 * the use of rotateLeft method of Integer class

public class IntegerRotateLeft {

	public static void main(String[] args) {
		int value = 12;
		// Get the binary equivalent
		out.println("Binary equivalent:"+Integer.toBinaryString(value));
		// get the  number of one's
		int rotatedLeft = Integer.rotateLeft(value, 3);
		out.println("New Value:"+rotatedLeft);
		out.println("Rotated Left Binary:"+Integer.toBinaryString(rotatedLeft));



Sample Output :

Running the rotateLeft() method example source code of Integer class will give you the following output

Binary equivalent:1100
New Value:96
Rotated Left Binary:1100000

Exception Scenario :


Similar Method :

  • N/A

Suggested Reading List :

References :