Home » Java Integer rotateLeft() method with Examples

Java Integer rotateLeft() method with Examples

by Online Tutorials Library

Java Integer rotateLeft() Method

The rotateLeft() method of Java Integer class 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).

Bit shifting is a bitwise operation which is performed on all the bits of a binary value by moving the bits to a definite number of places towards left or right.

Java has a single Logical left shift operator (<<).

Explanation:

Syntax:

Following is the declaration of rotateLeft () method:

Parameter:

DataType Parameter Description Required/Optional
int i Numeric value entered by a user whose bits are to be rotated left Required
int distance Number of bit positions (distance) to rotate left which is entered by a user. equired

Returns:

The rotateLeft () 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.

Exceptions:

NA

Compatibility Version:

Java 1.5 and above

Example 1

Test it Now

Output:

40 

Example 2

Test it Now

Output:

Binary equivalent: 10000 Value after left rotation: 128 New Binary value after Rotated Left: 10000000 

Example 3

Output:

Enter the Number: 16 Value: 16 Binary equivalent: 10000 Value: 32 Binary equivalent: 100000 Value: 128 Binary equivalent: 10000000 

Example 4

Test it Now

Output:

Value: -12 Value: -23 Value: -89 

You may also like