當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。