Signup/Sign In

Java Long reverse() Method

Java reverse() method is a part of the Long class of the java.lang package. This method is used to return the value which is obtained by reversing the order of the bits of the two's complement binary representation of the long value passed.


public static long reverse(long i)  


The parameter passed is the long value whose binary equivalent is to be reversed.


The value obtained by reversing the order of bits of the two's complement binary representation of the parameter passed.

Example 1:

Here, a positive and a negative number is taken for a better understanding of the method. The Long.toBinaryString() method is used for representing the long value in its binary equivalent.

import java.lang.Long;

public class StudyTonight
    public static void main(String[] args) 
          long a = 202L;  
          long b = -50L;  
          System.out.println("Original Number = " + a);             
          System.out.println("Binary Representation is = " + Long.toBinaryString(a)); //long value as a binary string 
          System.out.println("Number after reversal is:" + Long.reverse(a));//reversal of the number
          System.out.println("\n Original Number = " + b);               
          System.out.println("Binary Representation is = " + Long.toBinaryString(b));           
          System.out.println("Number after reversal is: " + Long.reverse(b));  

Original Number = 202
Binary Representation is = 11001010
Number after reversal is:5980780305148018688

Original Number = -50
Binary Representation is = 1111111111111111111111111111111111111111111111111111111111001110
Number after reversal is: 8358680908399640575

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.util.Scanner;  

public class StudyTonight 
    public static void main(String[] args) 
           System.out.print("Enter Original Value ");  
           Scanner sc = new Scanner(;  
           long i = sc.nextLong();  
           System.out.println("Actual Number = " + i);  
           System.out.println("Binary Representation = " + Long.toBinaryString(i)); // returns the long value as a binary equivalent 
           System.out.println("After reversing = " + Long.reverse(i)); //returns the value obtained by reversal of bits
        catch(Exception e)
           System.out.println("Invalid Input");

Enter Original Value 78
Actual Number = 78
Binary Representation = 1001110
After reversing = 8214565720323784704
Enter Original Value -50
Actual Number = -50
Binary Representation = 1111111111111111111111111111111111111111111111111111111111001110
After reversing = 8358680908399640575
Enter Original Value 0x4454
Invalid Input

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.

About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.