New Tutorials:   NUMPY    TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs

Java Long rotateRight() Method

Java rotateRight() 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 right (i.e towards lower-order) by the distance specified in the argument.

This method is also 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, rotateRight(10,1) refers to the rotation of a binary equivalent of 10 towards the right by one bit. The equivalent binary of 10 is 00001010, and by shifting 1 bit right it will be converted into 00000101 (i.e is equal to 5).

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

Syntax:

public static long rotateRight(long i, int distance) 

Parameters:

The parameters passed is the long value i who's binary equivalent bits are to be rotated and the 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 number passed as a parameter towards the right(i.e towards lower-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 = 8;
    long n2 = -8;
    int val = 3;

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

    for (int i = 0; i < val; i++) {
      n1 = Long.rotateRight(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.rotateRight(n2, val); //returns the value after rotation
      System.out.println(n2);
    }
  }
}


Binary equivalent is : 1000
1
2305843009213693952
288230376151711744
Binary equivalent is : 1111111111111111111111111111111111111111111111111111111111111000
2305843009213693951
-2017612633061982209
-252201579132747777

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.rotateRight(n, val); //returns the value after rotation
        System.out.println(n);
      }
    }
    catch(Exception e) {
      System.out.println("Invalid Input");
    }

  }
}


Enter the value and distance
67 4
Binary equivalent is : 1000011
3458764513820540932
4827858800541171712
301741175033823232
18858823439613952
****************************************
Enter the value and distance
-8 5
Binary equivalent is : 1111111111111111111111111111111111111111111111111111111111111000
-4035225266123964417
-126100789566373889
-3940649673949185
-123145302310913
-3848290697217

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.