Java - Integer reverseBytes() 方法



描述

Java Integer reverseBytes() 方法返回透過反轉指定 int 值的二進位制補碼錶示中的位元組順序而獲得的值。

宣告

以下是 java.lang.Integer.reverseBytes() 方法的宣告

public static int reverseBytes(int i)

引數

i − 這是 int 值。

返回值

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

異常

從正整數獲取反轉位整數示例

以下示例演示瞭如何使用 Integer reverseBytes() 方法透過反轉指定 int 值的二進位制補碼錶示中的位元組順序來獲取 int 值。我們建立了一個 int 變數併為其賦值一個正整數。然後使用 toBinaryString() 方法列印值的二進位制格式。使用 bitCount() 列印 1 位計數,然後使用 reverseBytes() 方法列印透過反轉指定 int 值中位元組順序獲得的值。

package com.tutorialspoint;
public class IntegerDemo {
   public static void main(String[] args) {
      int i = 170;
      System.out.println("Number = " + i);

      /* returns the string representation of the unsigned integer value 
         represented by the argument in binary (base 2) */
      System.out.println("Binary = " + Integer.toBinaryString(i));

      // returns the number of one-bit
      System.out.println("Number of one bit = " + Integer.bitCount(i)); 

      /*  returns the value obtained by reversing order of the bytes in 
         the specified int value */ 
      System.out.println("After reversing = " + Integer.reverseBytes(i));
   }
} 

輸出

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

Number = 170
Binary = 10101010
Number of one bit = 4
After reversing = -1442840576

從負整數獲取反轉位整數示例

以下示例演示瞭如何使用 Integer reverseBytes() 方法透過反轉指定負 int 值的二進位制補碼錶示中的位元組順序來獲取 int 值。我們建立了一個 int 變數併為其賦值一個負整數。然後使用 toBinaryString() 方法列印值的二進位制格式。使用 bitCount() 列印 1 位計數,然後使用 reverseBytes() 方法列印透過反轉指定 int 值中位元組順序獲得的值。

package com.tutorialspoint;
public class IntegerDemo {
   public static void main(String[] args) {
      int i = -170;
      System.out.println("Number = " + i);

      /* returns the string representation of the unsigned integer value 
         represented by the argument in binary (base 2) */
      System.out.println("Binary = " + Integer.toBinaryString(i));

      // returns the number of one-bit
      System.out.println("Number of one bit = " + Integer.bitCount(i)); 

      /*  returns the value obtained by reversing order of the bytes in 
         the specified int value */ 
      System.out.println("After reversing = " + Integer.reverseBytes(i));
   }
} 

輸出

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

Number = -170
Binary = 11111111111111111111111101010110
Number of one bit = 28
After reversing = 1459617791

從正零值獲取反轉位整數示例

以下示例演示瞭如何使用 Integer reverseBytes() 方法透過反轉指定零值的二進位制補碼錶示中的位元組順序來獲取 int 值。我們建立了一個 int 變數併為其賦值一個正整數。然後使用 toBinaryString() 方法列印值的二進位制格式。使用 bitCount() 列印 1 位計數,然後使用 reverseBytes() 方法列印透過反轉指定 int 值中位元組順序獲得的值。

package com.tutorialspoint;
public class IntegerDemo {
   public static void main(String[] args) {
      int i = 0;
      System.out.println("Number = " + i);

      /* returns the string representation of the unsigned integer value 
         represented by the argument in binary (base 2) */
      System.out.println("Binary = " + Integer.toBinaryString(i));

      // returns the number of one-bit
      System.out.println("Number of one bit = " + Integer.bitCount(i)); 

      /*  returns the value obtained by reversing order of the bytes in 
         the specified int value */ 
      System.out.println("After reversing = " + Integer.reverseBytes(i));
   }
} 

輸出

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

Number = 0
Binary = 0
Number of one bit = 0
After reversing = 0
java_lang_integer.htm
廣告