Java - Long reverseBytes() 方法



描述

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

宣告

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

public static long reverseBytes(long i)

引數

i − 這是 long 值。

返回值

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

異常

從包含正值的 long 獲取反轉的 long 值示例

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

package com.tutorialspoint;

public class LongDemo {
   public static void main(String[] args) {
      long i = 170L;
      System.out.println("Number = " + i);

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

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

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

輸出

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

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

從包含負值的 long 獲取反轉的 long 值示例

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

package com.tutorialspoint;

public class LongDemo {
   public static void main(String[] args) {
      long i = -170L;
      System.out.println("Number = " + i);

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

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

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

輸出

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

Number = -170
Binary = 1111111111111111111111111111111111111111111111111111111101010110
Number of one bit = 60
After reversing = 6269010681299730431

從包含零值的 long 獲取反轉的 long 值示例

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

package com.tutorialspoint;

public class LongDemo {
   public static void main(String[] args) {
      long i = 0L;
      System.out.println("Number = " + i);

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

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

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

輸出

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

Number = 0
Binary = 0
Number of one bit = 0
After reversing = 0
java_lang_long.htm
廣告
© . All rights reserved.