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 :
// 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)); } } |
Output:
The number after reversing bit= 1152921504606846976 The number after reversing bit= 9151314442816847872
Program 2 : When a negative number is passed
// 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)); } } |
Output:
The number after reversing bit= 2305843009213693951 The number after reversing bit= 4683743612465315839
Program 3 : When a decimal number is passed
// 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)); } } |
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
// 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")); } } |
Output:
prog.java:16: error: incompatible types: String cannot be converted to long
+ Long.reverse("12"));
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java 8 | ArrayDeque removeIf() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java Clock tickMinutes() method in Java with Examples
- Java Clock withZone() method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Split() String method in Java with examples
- DelayQueue add() method in Java with Examples
- Dictionary size() Method in Java with Examples
- Constructor getAnnotatedReturnType() method in Java with Examples
- Constructor getAnnotatedReceiverType() method in Java with Examples
- Java Math random() method with Examples
- Collections checkedMap() method in Java with Examples
- Duration from(TemporalUnit) method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh

