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
In the
RotateLeft.javafile, 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
toBinaryStringmethod is used to get the binary equivalent representation of integer numbers.Save the file and close the text editor.
Compile the code using the following command:
javac ~/project/RotateLeft.javaExecute the code using the following command:
java -cp ~/project RotateLeftIn 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
In the
RotateLeft.javafile, 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)); } }Save the file and close the text editor.
Compile the code using the following command:
javac ~/project/RotateLeft.javaExecute the code using the following command:
java -cp ~/project RotateLeftWhen prompted, enter an integer number and the number of bit positions to rotate the bits to the left.
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.



