-
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));
}
}
-
Save the file and close the text editor.
-
Compile the code using the following command:
javac ~/project/RotateLeft.java
-
Execute the code using the following command:
java -cp ~/project RotateLeft
-
When 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.