Introduction
In this lab, we will learn about the Java reverseBytes()
method of the Long
class, which is used to reverse the order of bytes of the two's complement binary representation of the long
value passed.
In this lab, we will learn about the Java reverseBytes()
method of the Long
class, which is used to reverse the order of bytes of the two's complement binary representation of the long
value passed.
First, we need to create a Java class in the ~/project
directory. Let's name it ReverseBytesDemo.java
.
public class ReverseBytesDemo {
}
Next, we need to add the main
method to our class. This will be the entry point for our program.
public static void main(String[] args) {
}
reverseBytes()
methodNow, let's use the reverseBytes()
method to reverse the bytes of a long
value.
long a = 342;
long reversedValue = Long.reverseBytes(a);
System.out.println("Original value: " + a);
System.out.println("Reversed value: " + reversedValue);
This code will output:
Original value: 342
Reversed value: 6197234562238513152
We can also take user input and reverse the bytes of the input value. Let's modify our code to take user input.
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
long userInput = scanner.nextLong();
long reversedValue = Long.reverseBytes(userInput);
System.out.println("Original value: " + userInput);
System.out.println("Reversed value: " + reversedValue);
This code prompts the user to enter a number and then reverses the bytes of the input value. Run the code using javac ReverseBytesDemo.java && java ReverseBytesDemo
command.
We can handle any exceptions that may occur when taking user inputs by using a try-catch
block.
Scanner scanner = new Scanner(System.in);
try {
System.out.print("Enter a number: ");
long userInput = scanner.nextLong();
long reversedValue = Long.reverseBytes(userInput);
System.out.println("Original value: " + userInput);
System.out.println("Reversed value: " + reversedValue);
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a valid number.");
}
This code uses a try-catch
block to handle any exceptions that may occur when the user enters an invalid input.
Let's test the reverseBytes()
method with negative number.
long a = -23;
long reversedValue = Long.reverseBytes(a);
System.out.println("Original value: " + a);
System.out.println("Reversed value: " + reversedValue);
This code will output:
Original value: -23
Reversed value: -1585267068834414593
We can also output the binary representation of the original and reversed values using the Long.toBinaryString()
method.
System.out.println("Original value in binary: " + Long.toBinaryString(a));
System.out.println("Reversed value in binary: " + Long.toBinaryString(reversedValue));
This code will output:
Original value in binary: 1111111111111111111111111111111111111111111111111111111111101001
Reversed value in binary: 1010101100000000000000000000000000000000000000000000000000000000
Note that the binary representation of the reversed value is the original binary representation with each group of 8 bits reversed.
Let's write a user-defined function to encapsulate the reverseBytes()
method.
public class ReverseBytesDemo {
public static long reverseBytes(long value) {
return Long.reverseBytes(value);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.print("Enter a number: ");
long userInput = scanner.nextLong();
long reversedValue = reverseBytes(userInput);
System.out.println("Original value: " + userInput);
System.out.println("Reversed value: " + reversedValue);
System.out.println("Original value in binary: " + Long.toBinaryString(userInput));
System.out.println("Reversed value in binary: " + Long.toBinaryString(reversedValue));
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a valid number.");
}
}
}
Now that we have our user-defined function set up, we can test it by running the modified main()
method again.
Scanner scanner = new Scanner(System.in);
try {
System.out.print("Enter a number: ");
long userInput = scanner.nextLong();
long reversedValue = reverseBytes(userInput);
System.out.println("Original value: " + userInput);
System.out.println("Reversed value: " + reversedValue);
System.out.println("Original value in binary: " + Long.toBinaryString(userInput));
System.out.println("Reversed value in binary: " + Long.toBinaryString(reversedValue));
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a valid number.");
}
Run the code using javac ReverseBytesDemo.java && java ReverseBytesDemo
command.
In this lab, we learned about the Java reverseBytes()
method of the Long
class, which is used to reverse the order of bytes of the two's complement binary representation of the long
value passed. We also learned how to use user input, handle exceptions, output binary representation, and encapsulate the method in a user-defined function. By following these steps, you should now be able to use the reverseBytes()
method in your own Java programs.