Java.lang Package Classes

Java Long - rotateRight() Method



The java.lang.Long.rotateRight() method returns the value obtained by rotating the two's complement binary representation of the specified long value right by the specified number of bits. (Bits shifted out of the right hand, or low-order, side reenter on the left, or high-order.)

Syntax

public static long rotateRight(long i,
                               int distance)

Parameters

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

Return Value

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

Exception

NA.

Example:

In the example below, the java.lang.Long.rotateRight() method returns the value which is obtained by rotating the two's complement binary representation of the given long value right 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 right
    System.out.println("The result is = " + Long.rotateRight(x, 2)); 
    System.out.println("The result is = " + Long.rotateRight(y, 3));  
  }
}

The output of the above code will be:

The result is = -4611686018427387892
The result is = 6917529027641081887

❮ Java.lang - Long