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


Java Java lang.Long.reverse()用法及代碼示例



java.lang.Long.reverse()是Java中的內置函數,該函數返回通過反轉指定long值的二進製補碼二進製表示形式中的位順序而獲得的值。

用法:

public static long reverse(long num) 
Parameter:
num - the number passed
返回:
the value obtained by reversing the order of the bits in the 
two's complement binary representation of the specified long value.

例子:


Input:254 
Output:9151314442816847872

Input:8
Output:1152921504606846976

以下示例程序旨在說明java.lang.Long.reverse()函數:

程序1:

// Java program that demonstrates the 
// Long.reverse() function 
  
// include lang package 
import java.lang.*; 
  
public class GFG { 
  
public static void main(String[] args) 
    { 
  
        long l = 8; 
  
        System.out.println("The number after reversing bit= "
                           + Long.reverse(l)); 
  
        l = 254; 
        System.out.println("The number after reversing bit= "
                           + Long.reverse(l)); 
    } 
}

輸出:

The number after reversing bit= 1152921504606846976
The number after reversing bit= 9151314442816847872

程序2:當傳遞負數時

// Java program that demonstrates the 
// Long.reverse() function 
// negative number 
  
// include lang package 
import java.lang.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        long l = -8; 
  
        System.out.println("The number after reversing bit= "
                           + Long.reverse(l)); 
  
        l = -254; 
        System.out.println("The number after reversing bit= "
                           + Long.reverse(l)); 
    } 
}

輸出:

The number after reversing bit= 2305843009213693951
The number after reversing bit= 4683743612465315839

程序3:傳遞十進製數時

// Java program that demonstrates the 
// Long.reverse() function 
// decimal number 
  
// include lang package 
import java.lang.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        System.out.println("The number after reversing bit= "
                           + Long.reverse(11.34)); 
    } 
}

輸出:

prog.java:16:error:incompatible types:possible lossy conversion from double to long
                           + Long.reverse(11.34));

程序4::當傳遞字符串號時

// Java program that demonstrates the 
// Long.reverse() function 
// string number 
  
// include lang package 
import java.lang.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        System.out.println("The number after reversing bit= "
                           + Long.reverse("12")); 
    } 
}

輸出:

prog.java:16:error:incompatible types:String cannot be converted to long
                           + Long.reverse("12"));


相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Java lang.Long.reverse() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。