The rotateRight() method of Java Integer class returns the value obtained by rotating the 2’s complement binary representation of the given integer value right by the specified number of bits.
Syntax
public static int rotateRight (int i , int distance)
Parameters
The parameter ‘i’ represents the bytes whose value is to be reversed and parameter ‘distance’ represents the number of bit positions to rotate right.
Return Value
This method returns the value obtained by rotating the 2’s complement binary representation of the integer value right by the specified number of bits.
Example 1
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class JavaIntegerRotateRightExample1 { public static void main(String[] args) { Integer i=120; /* returns the value obtained by rotating the 2’s complement binary representation of the given integer value right by the specified number of bits*/ int left1= Integer.rotateRight(i,1000); System.out.println(left1); //passing negative distance System.out.println(Integer.rotateRight(12,-12)); } } |
Output
1 2 3 4 |
2013265920 49152 |
Example 2
1 2 3 4 5 6 7 8 9 10 |
public class JavaIntegerRotateRightExample2 { public static void main(String[] args) { Integer i=120; //returns the same value if distance is passed as zero int left1= Integer.rotateRight(i,0); System.out.println(left1); } } |
Output
1 2 3 |
120 |
Example 3
1 2 3 4 5 6 7 8 9 10 |
public class JavaIntegerRotateRightExample3 { public static void main(String[] args) { Integer i=0; //returns 0 same value if the value of i is 0 int left1= Integer.rotateRight(i,Integer.MAX_VALUE); System.out.println(left1); } } |
Output
1 2 3 |
0 |