Java Integer RotateLeft Method

JavaJavaBeginner
Practice Now

Introduction

Java rotateLeft() method is a built-in method in the Integer class of the java.lang package. In this lab, you will learn how to use the rotateLeft() method, which is used to rotate the bits of a binary equivalent of the integer number, i.e., all bits of the binary number are shifted left or right to a definite number of places.


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/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") 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/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-117740{{"`Java Integer RotateLeft Method`"}} java/classes_objects -.-> lab-117740{{"`Java Integer RotateLeft Method`"}} java/class_methods -.-> lab-117740{{"`Java Integer RotateLeft Method`"}} java/modifiers -.-> lab-117740{{"`Java Integer RotateLeft Method`"}} java/oop -.-> lab-117740{{"`Java Integer RotateLeft Method`"}} java/packages_api -.-> lab-117740{{"`Java Integer RotateLeft Method`"}} java/user_input -.-> lab-117740{{"`Java Integer RotateLeft Method`"}} java/wrapper_classes -.-> lab-117740{{"`Java Integer RotateLeft Method`"}} java/identifier -.-> lab-117740{{"`Java Integer RotateLeft Method`"}} java/arrays -.-> lab-117740{{"`Java Integer RotateLeft Method`"}} java/comments -.-> lab-117740{{"`Java Integer RotateLeft Method`"}} java/data_types -.-> lab-117740{{"`Java Integer RotateLeft Method`"}} java/operators -.-> lab-117740{{"`Java Integer RotateLeft Method`"}} java/output -.-> lab-117740{{"`Java Integer RotateLeft Method`"}} java/strings -.-> lab-117740{{"`Java Integer RotateLeft Method`"}} java/variables -.-> lab-117740{{"`Java Integer RotateLeft Method`"}} java/system_methods -.-> lab-117740{{"`Java Integer RotateLeft Method`"}} end

Using the rotateLeft() method on a negative number

  1. In the RotateLeft.java file, modify the Java code to include a negative number. Assuming the integer number is -10, write the code below:

    public class RotateLeft {
        public static void main(String[] args) {
            int n = -10;
            int value = 4;
            System.out.println("Binary equivalent of " + n + " is: " + Integer.toBinaryString(n));
            n = Integer.rotateLeft(n, value); //rotate left value times
            System.out.println("After rotating " + value + " times to the left, the decimal number is: " + n);
            System.out.println("Binary equivalent of the new number is: " + Integer.toBinaryString(n));
        }
    }

    Note: The toBinaryString method is used to get the binary equivalent representation of integer numbers.

  2. Save the file and close the text editor.

  3. Compile the code using the following command:

    javac ~/project/RotateLeft.java
  4. Execute the code using the following command:

    java -cp ~/project RotateLeft
  5. In the output, you should see the binary equivalent of the original integer number, the decimal equivalent of the new number after rotating the specified number of times to the left, and the binary equivalent of the new number.

Using the rotateLeft() method for a user-defined number

  1. In the RotateLeft.java file, modify the Java code to take user-defined values for integer number and the number of bit positions to rotate the bits to the left. Write the code as shown below:

    import java.util.Scanner;
    
    public class RotateLeft {
        public static void main(String[] args) {
    
            Scanner input = new Scanner(System.in);
            System.out.print("Enter an integer number: ");
            int n = input.nextInt();
    
            System.out.print("Enter the number of times to rotate the bits to the left: ");
            int value = input.nextInt();
    
            input.close();
    
            System.out.println("Binary equivalent of " + n + " is: " + Integer.toBinaryString(n));
    
            n = Integer.rotateLeft(n, value); //rotate left value times
    
            System.out.println("After rotating " + value + " times to the left, the decimal number is: " + n);
            System.out.println("Binary equivalent of the new number is: " + Integer.toBinaryString(n));
        }
    }
  2. Save the file and close the text editor.

  3. Compile the code using the following command:

    javac ~/project/RotateLeft.java
  4. Execute the code using the following command:

    java -cp ~/project RotateLeft
  5. When prompted, enter an integer number and the number of bit positions to rotate the bits to the left.

  6. In the output, you should see the binary equivalent of the original integer number, the decimal equivalent of the new number after rotating the specified number of times to the left, and the binary equivalent of the new number.

Summary

In this lab, you learned how to use the rotateLeft() method in the Integer class to rotate the bits of an integer's binary equivalent, allowing you to output the binary equivalent of the original integer number, and binary equivalent of the new number after rotation. By executing different values, you can observe how the rotateLeft() method shifts the bits of an integer's binary equivalent all bits to the left.

Other Java Tutorials you may like