Short类的reverseBytes()方法是Java中的内置方法,用于返回通过反转指定的short值的二进制补码表示形式的字节顺序而获得的值。
用法:
public static short reverseBytes(short a)
参数:该方法采用一个简短类型的参数a,其字节将被反转。
返回值:该方法将返回通过反转指定的short值中的字节获得的值。
例子:
Input: 75
Output: 1258291200
Explanation:
Consider an short a = 75
Binary Representation = 1001011
Number of one bit = 4
After reversing the bytes = 1258291200
Input: -43
Output: -704643073
以下示例程序旨在说明Short.reverseBytes()方法:
示例1:为正数。
// Java program to illustrate the
// Short.reverseBytes() method
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
// Create a Short instance
short a = 61;
// Print the value before reversal of Bytes
System.out.println("Original value = " + a);
// Reverse the bytes in the specified short value
// Using reverseBytes() method
System.out.println("After reversing the bytes : \n"
+ "New value : "
+ Short.reverseBytes(a));
}
}
输出:
Original value = 61
After reversing the bytes :
New value : 15616
示例2:为负数。
// Java program to illustrate the
// Short.reverseBytes() method
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
// Create a Short instance
short a = -45;
// Print the value before reversal of Bytes
System.out.println("Original value = " + a);
// Reverse the bytes in the specified short value
// Using reverseBytes() method
System.out.println("After reversing the bytes : \n"
+ "New value : "
+ Short.reverseBytes(a));
}
}
输出:
Original value = -45
After reversing the bytes :
New value : -11265