Dark Mode On/Off

# Java Integer rotateRight() Method

Java `rotateRight()` method is a part of the `Integer` 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 left it will be converted into 00000101 (i.e is equal to 5).

### Syntax:

``public static int rotateRight(int i, int distance) ``

### Parameters:

The parameters passed are integer value who's binary equivalent bits are to be rotated and int distance to specify the number of positions 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 `Integer.toBinaryString()` method is used for representing the number in its binary equivalent.

``````import java.lang.*;
public class StudyTonight
{
public static void main(String[] args)
{
int n1 = 8;
int n2 = -8;
int val = 3;
System.out.println("Binary equivalent is : " + Integer.toBinaryString(n1));
for(int i = 0; i < val; i++)
{
n1 = Integer.rotateRight(n1, val); //returns the value after rotation
System.out.println(n1);
}
System.out.println("Binary equivalent is : " + Integer.toBinaryString(n2));

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

Binary equivalent is : 1000
1
536870912
67108864
Binary equivalent is : 11111111111111111111111111111000
536870911
-469762049
-58720257

## 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);
int n = sc.nextInt();
int val = sc.nextInt();
System.out.println("Binary equivalent is : " + Integer.toBinaryString(n));
for(int i = 0; i < val; i++)
{
n = Integer.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
6 3
Binary equivalent is : 110
-1073741824
402653184
50331648
*******************************
Enter the value and distance
-6 3
Binary equivalent is : 11111111111111111111111111111010
1610612735
-335544321
-41943041
************************************
Enter the value and distance
0x56 3
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.