Java.lang Package Classes

Java Long - rotateLeft() Method



The java.lang.Long.rotateLeft() method returns the value obtained by rotating the two's complement binary representation of the specified long 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.)

Syntax

public static long rotateLeft(long i,
                              int distance)

Parameters

i Specify the value whose bits are to be rotated left.
distance Specify the number of bit positions to rotate left.

Return Value

Returns the value obtained by rotating the two's complement binary representation of the specified long value left by the specified number of bits.

Exception

NA.

Example:

In the example below, the java.lang.Long.rotateLeft() method returns the value which is obtained by rotating the two's complement binary representation of the given long value left by the given number of bits.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating long values
    long x = 51;
    long y = 251;

    //printing the values obtained by rotating left
    System.out.println("The result is = " + Long.rotateLeft(x, 2)); 
    System.out.println("The result is = " + Long.rotateLeft(y, 3));  
  }
}

The output of the above code will be:

The result is = 204
The result is = 2008

❮ Java.lang - Long