The source code to swap bytes of an integer number is given below. The given program is compiled and executed successfully.
// Java program to swap bytes of
// an integer number
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int num = 0x4567;
System.out.printf("Number before swapping : %04X\n", num);
num = ((num << 8) & 0xff00) | ((num >> 8) & 0x00ff);
System.out.printf("Number after swapping : %04X\n", num);
}
}
Output:
Number before swapping : 4567
Number after swapping : 6745
Explanation:
In the above program, we imported the "java.util.Scanner" package to read input from the user. And, created a public class Main. It contains a static method main().
The main() method is an entry point for the program. Here, we created a variable num initialized with 0X4567. Then we swapped the bytes of the number and printed the result.
Program/Source Code:
The source code to swap bytes of an integer number is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we imported the "java.util.Scanner" package to read input from the user. And, created a public class Main. It contains a static method main().
The main() method is an entry point for the program. Here, we created a variable num initialized with 0X4567. Then we swapped the bytes of the number and printed the result.
need an explanation for this answer? contact us directly to get an explanation for this answer