VOOZH about

URL: https://www.geeksforgeeks.org/java/java-lang-long-reverse-method-java-examples/

⇱ Java lang.Long.reverse() method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java lang.Long.reverse() method in Java with Examples

Last Updated : 23 May, 2019
java.lang.Long.reverse() is a built-in function in Java which returns the value obtained by reversing the order of the bits in the two's complement binary representation of the specified long value. Syntax:
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.
Examples:
Input : 254 
Output : 9151314442816847872

Input : 8
Output : 1152921504606846976
The program below illustrates the java.lang.Long.reverse() function: Program 1 : Output:
The number after reversing bit= 1152921504606846976
The number after reversing bit= 9151314442816847872
Program 2 : When a negative number is passed Output:
The number after reversing bit= 2305843009213693951
The number after reversing bit= 4683743612465315839
Program 3 : When a decimal number is passed Output:
prog.java:16: error: incompatible types: possible lossy conversion from double to long
 + Long.reverse(11.34));
Program 4 : When a string number is passed
Output:
prog.java:16: error: incompatible types: String cannot be converted to long
 + Long.reverse("12"));
Comment