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.

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