Signup/Sign In
LAST UPDATED: OCTOBER 5, 2020

Java Long rotateLeft() Method

Java rotateLeft() method is a part of the Long 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).

It must be noted that left rotation with a negative distance is equivalent to right rotation: rotateLeft(val, -distance) == rotateRight(val, distance).

Syntax:

public static long rotateLeft(long i, int distance) 

Parameters:

The parameters passed is a long value i who's binary equivalent bits are to be rotated and an integer distance to specify the number of position the bits are to be rotated.

Returns:

Returns the value obtained by rotating two's complement of the binary equivalent of the long value passed as 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 Long.toBinaryString() method is used for representing the number in its binary equivalent.

import java.lang. * ;

public class StudyTonight {

  public static void main(String[] args) {

    long n1 = 4L;
    long n2 = -4L;
    int val = 3;

    System.out.println("Binary equivalent is : " + Long.toBinaryString(n1));

    for (int i = 0; i < val; i++) {
      n1 = Long.rotateLeft(n1, val); //returns the value after rotation
      System.out.println(n1);
    }

    System.out.println("Binary equivalent is : " + Long.toBinaryString(n2));

    for (int i = 0; i < val; i++) {
      n2 = Long.rotateLeft(n2, val); //returns the value after rotation
      System.out.println(n2);
    }
  }
}


Binary equivalent is : 100
32
256
2048
Binary equivalent is : 1111111111111111111111111111111111111111111111111111111111111100
-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 );
      long n = sc.nextLong();
      int val = sc.nextInt();

      System.out.println("Binary equivalent is : " + Long.toBinaryString(n));

      for (int i = 0; i < val; i++) {
        n = Long.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
8 4
Binary equivalent is : 1000
128
2048
32768
524288
**********************************
Enter the value and distance
-8 6
Binary equivalent is : 1111111111111111111111111111111111111111111111111111111111111000
-449
-28673
-1835009
-117440513
-7516192769
-481036337153
***********************************
Enter the value and distance
0x56 2
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.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.