The Wayback Machine - https://web.archive.org/web/20240118165456/https://www.geeksforgeeks.org/integer-rotateright-method-in-java/
Open In App
Related Articles

Integer rotateRight() Method in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Bit shifting is used in programming and has at least one variation in each programming language. Java has a single Logical right shift operator (>>). Bit shifting is a type of bitwise operation. Bit shifting is done on all the bits of a binary value which moves the bits by a definite number of places to the right. If the value 0100; (ie. 4) is shifted right, it becomes 0010; (ie. 2)which is again shifted to the right again it becomes 0001; or 1.
The java.lang.Integer.rotateRight() is the method which returns the value we get by rotating the two’s complement binary representation of the specified int value a towards the right by a specified number of bits. Bits are shifted towards the right hand i.e., the lower-order.

Syntax :

public static int rotateRight(int a, int shifts)

Parameters: The method takes two parameters:

  • a : This is of integer type and refers to the value on which operation is to be performed.
  • shifts : This is also of integer type and refers to the distance of rotation.

Returns: rotateRight() method returns the value obtained by rotating the two’s complement binary representation of the specified int value right by the specified number of bits.

Below programs illustrate the Java.lang.Integer.rotateRight() method:
Program 1: For a positive number.




// Java program to illustrate the
// Java.lang.Integer.rotateRight() method
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
        int a = 64;
        int shifts = 0;
        while (shifts < 3)
        // It will return the value obtained by rotating left
        {
            a = Integer.rotateRight(a, 2);
            System.out.println(a);
            shifts++;
        }
    }
}


Output:

16
4
1

Program 2: For a negative number.




// Java program to illustrate the
// Java.lang.Integer.rotateRight() method
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        int a = -165;
        int shifts = 0;
        while (shifts < 3)
        // It will return the value obtained by rotating left
        {
            a = Integer.rotateRight(a, 2);
            System.out.println(a);
            shifts++;
        }
    }
}


Output:

-42
-1073741835
1879048189

Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 18 Oct, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials