Java - 簡短的 reverseBytes() 方法



Java Short reverseBytes() 方法用於檢索透過反轉給定 short 值的二進位制 2 的補碼形式中的位元組順序獲得的原始 short 值。

reverseBytes() 方法是靜態的,這意味著呼叫此方法時應新增類名作為字尾。如果我們非靜態地呼叫此方法,我們將遇到編譯錯誤。非靜態方法通常透過簡單地宣告 method_name(argument) 來呼叫。

語法

以下是Java Short parseShort() 方法的語法:

public static short reverseBytes(short i)

引數

  • i − 這是 short 值。

返回值

此方法返回透過反轉指定 short 值中的位元組獲得的值。

從正 Short 值獲取反向位元組示例

我們可以使用 reverseBytes() 方法傳遞正值。結果將是給定 short 值的反向位元組值。

以下示例顯示了 Java Short reverseBytes() 方法的使用。在這裡,我們將一個正值賦給 short 變數“shortNum”。然後顯示反轉位元組順序後的 short 值。

package com.tutorialspoint;

public class ShortDemo {
   public static void main(String[] args) {
      
      // assign value to short
      short shortNum = 50;
      
      /* returns the value after reversing the order of the bytes
      in the binary representation of the specified short value. */
      short shortValue = Short.reverseBytes(shortNum);   
      
      // displaying the reversed value
      System.out.println("The reversed value is = " + shortValue);
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

The reversed value is = 12800

從負 Short 值獲取反向位元組示例

我們也可以使用 reverseBytes() 方法傳遞負值。結果將是給定 short 值的反向位元組值。

package com.tutorialspoint;

public class ShortDemo {
   public static void main(String[] args) {
      
      // assign value to short
      short shortNum = -87;
      
      /*
      * returns the value after reversing the order of the bytes
      * in the binary representation of the specified short value.
      */
      short shortValue = Short.reverseBytes(shortNum);
      
      // displaying the reversed value
      System.out.println("The reversed value is = " + shortValue);
   }
}

輸出

以下是上述程式碼的輸出:

The reversed value is = -22017

從最大 Short 值獲取反向位元組示例

我們也可以使用此方法傳遞 MAX_VALUE 作為引數,它返回-129。

在下面的示例中,建立了一個 short 變數“s”。MAX_VALUE 被賦給此變數的值。然後返回給定 short 值的 reverseBytes:

package com.tutorialspoint;

public class ShortDemo {
   public static void main(String[] args) {
      
      // create short value
      short s = Short.MAX_VALUE;
      System.out.println("The short MAX_VALUE is: " + s);
      
      // create reverseBytes of the given short value
      short x = Short.reverseBytes(s);
      
      // print the reverseBytes of the given short value
      System.out.println("reverseBytes of s is: " + x);
   }
}

輸出

上述程式碼的輸出如下:

The short MAX_VALUE is: 32767
reverseBytes of s is: -129

從最小 Short 值獲取反向位元組示例

如果我們使用此方法傳遞 MIN_VALUE 作為引數,它將返回 128。

在下面給出的示例中,建立了一個 short 變數“s”。MIN_VALUE 被賦給此變數的值。然後返回給定 short 值的 reverseBytes:

package com.tutorialspoint;

public class ShortDemo {
   public static void main(String[] args) {
      
      // create short value
      short s = Short.MIN_VALUE;
      System.out.println("The short MIN_VALUE is: " + s);
      
      // create reverseBytes of the given short value
      short x = Short.reverseBytes(s);
      
      // print the reverseBytes of the given short value
      System.out.println("reverseBytes of s is: " + x);
   }
}  

輸出

執行上述程式碼時,我們得到以下輸出:

The short MIN_VALUE is: -32768
reverseBytes of s is: 128
java_lang_short.htm
廣告