当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Short reverseBytes()用法及代码示例



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


相关用法


注:本文由纯净天空筛选整理自Sruti Rai大神的英文原创作品 Short reverseBytes() in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。