Java Integer rotateRight Method

JavaJavaBeginner
Practice Now

Introduction

The rotateRight() method is useful for performing bit manipulations on binary data. Specifically, it rotates the binary digits of a number to the right by a specified number of bits. This can be useful for performing bitwise operations on integers or converting between binary and decimal numbers. In this lab, we will explore how to use this method to rotate a number of different integers and examine the results.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/for_loop("`For Loop`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117742{{"`Java Integer rotateRight Method`"}} java/classes_objects -.-> lab-117742{{"`Java Integer rotateRight Method`"}} java/class_methods -.-> lab-117742{{"`Java Integer rotateRight Method`"}} java/exceptions -.-> lab-117742{{"`Java Integer rotateRight Method`"}} java/modifiers -.-> lab-117742{{"`Java Integer rotateRight Method`"}} java/oop -.-> lab-117742{{"`Java Integer rotateRight Method`"}} java/user_input -.-> lab-117742{{"`Java Integer rotateRight Method`"}} java/wrapper_classes -.-> lab-117742{{"`Java Integer rotateRight Method`"}} java/identifier -.-> lab-117742{{"`Java Integer rotateRight Method`"}} java/arrays -.-> lab-117742{{"`Java Integer rotateRight Method`"}} java/comments -.-> lab-117742{{"`Java Integer rotateRight Method`"}} java/data_types -.-> lab-117742{{"`Java Integer rotateRight Method`"}} java/for_loop -.-> lab-117742{{"`Java Integer rotateRight Method`"}} java/operators -.-> lab-117742{{"`Java Integer rotateRight Method`"}} java/output -.-> lab-117742{{"`Java Integer rotateRight Method`"}} java/strings -.-> lab-117742{{"`Java Integer rotateRight Method`"}} java/variables -.-> lab-117742{{"`Java Integer rotateRight Method`"}} java/system_methods -.-> lab-117742{{"`Java Integer rotateRight Method`"}} end

Import Required Packages

In this first step, we need to import the required package to use the rotateRight() method. For this, we will use the java.lang and java.util packages.

import java.lang.*;
import java.util.*;

This package contains the Scanner class which we need for taking user input.

Define the Main Class

In this step, we will define the RotateRight class and the main() method.

public class RotateRight {
    public static void main(String[] args) {
        // Code to be added here
    }
}

Define the Variables

In this step, we will define the required variables for our program.

public class RotateRight {
    public static void main(String[] args) {
        int n1 = 8;
        int n2 = -8;
        int val = 3;
        // Code to be added here
    }
}

We have defined three variables n1, n2, and val. These variables will be used for performing bit rotating operations in later steps.

Output Binary Equivalent

In this step, we will output the binary equivalent of both numbers n1 and n2 by using the Integer.toBinaryString() method.

public class RotateRight {
    public static void main(String[] args) {
        int n1 = 8;
        int n2 = -8;
        int val = 3;

        System.out.println("Binary equivalent of n1: " + Integer.toBinaryString(n1));
        System.out.println("Binary equivalent of n2: " + Integer.toBinaryString(n2));
    }
}

Here, we are using the Integer.toBinaryString() method to convert integers into binary format and then printing them using the System.out.println() method.

Perform Bit Rotating Operation

In this step, we will use the rotateRight() method to perform bit rotating operations.

public class RotateRight {
    public static void main(String[] args) {
        int n1 = 8;
        int n2 = -8;
        int val = 3;

        System.out.println("Binary equivalent of n1: " + Integer.toBinaryString(n1));
        for (int i = 0; i < val; i++) {
            n1 = Integer.rotateRight(n1, val);
            System.out.println(n1);
        }

        System.out.println("Binary equivalent of n2: " + Integer.toBinaryString(n2));
        for (int i = 0; i < val; i++) {
            n2 = Integer.rotateRight(n2, val);
            System.out.println(n2);
        }
    }
}

The above code has two for loops to perform bit rotation operations on both numbers. We have used the rotateRight() method along with the number to be rotated and the number of positions to rotate.

Take User Input to Perform Bit Rotation

In this step, we will take user input through the command line and perform bit rotating operations based on user input.

public class RotateRight {
    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);
                System.out.println(n);
            }
        } catch (Exception e) {
            System.out.println("Invalid Input");
        }
    }
}

Here, we have taken user input through the scanner object sc nextInt() method. Then, bit rotating operations will be performed as per user inputs.

Compile and Run the Program

To compile and run the program, we can use the following command in the terminal:

javac RotateRight.java && java RotateRight

Summary

In this lab, we learned how to perform bit rotating operations in Java using the Integer.rotateRight() method. We started by importing the required packages, defining the main class, and user-defined variables. We output the binary equivalent of the given input numbers and then performed bit rotating operations based on user input. Finally, we compiled and ran the program and checked the output.

Other Java Tutorials you may like