Java Java () 方法与示例
Java .lang.Long.reverse()是Java中的一个内置函数,它返回通过反转指定long 值的二进制补码表示中的位的顺序而获得的值。
句法:
public static long reverse(long num)
Parameter :
num - the number passed
Returns :
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"));