Integer reverse() Method In Java
Last Updated :
07 Jul, 2018
Improve
The java.lang.Integer.reverse() is an inbuilt method in Java and is used to return the reverse order of the bits in the two's complement binary representation of the specified int value.
Syntax:
java
java
java
public static int reverse(int a)Parameters: The parameter a is an integer value whose bits are to be reversed. Return Value: The method returns the value which is obtained by reversing the order of the bits in the specified int value. Examples:
Input: 86 Output: 1778384896 Explanation: Consider an integer a = 86 Binary Representation = 1010110 The number of one bit = 4 After reversing it is = 1778384896 Input: 168 Output: 352321536Below programs illustrate the java.lang.Integer.reverse() method: Program 1: For a positive number.
// Java program to illustrate the
// Java.lang.Integer.reverse() method
import java.lang.*;
public class geeks {
public static void main(String[] args)
{
int a = 168;
System.out.println("Number = " + a);
// It returns the value obtained by reversing order of the bits
System.out.println("By reversing we get = " + Integer.reverse(a));
}
}
16
1
// Java program to illustrate the
2
// Java.lang.Integer.reverse() method
3
import java.lang.*;
4
5
public class geeks {
6
7
public static void main(String[] args)
8
{
9
10
int a = 168;
11
System.out.println("Number = " + a);
12
13
// It returns the value obtained by reversing order of the bits
14
System.out.println("By reversing we get = " + Integer.reverse(a));
15
}
16
}
Output:
Program 2: For a Negative number.
Number = 168 By reversing we get = 352321536
// Java program to illustrate the
// Java.lang.Integer.reverse() method
import java.lang.*;
public class geeks {
public static void main(String[] args)
{
int a = -72;
System.out.println("Number = " + a);
// It returns the value obtained by reversing order of the bits
System.out.println("By reversing we get = " + Integer.reverse(a));
}
}
16
1
// Java program to illustrate the
2
// Java.lang.Integer.reverse() method
3
import java.lang.*;
4
5
public class geeks {
6
7
public static void main(String[] args)
8
{
9
10
int a = -72;
11
System.out.println("Number = " + a);
12
13
// It returns the value obtained by reversing order of the bits
14
System.out.println("By reversing we get = " + Integer.reverse(a));
15
}
16
}
Output:
Program 3: For a decimal value and string.
Note: It throws an error message when a decimal value or string is passed as an argument.
Number = -72 By reversing we get = 503316479
// Java program to illustrate the
// Java.lang.Integer.reverse() method
import java.lang.*;
public class geeks {
public static void main(String[] args)
{
int a = 37.9;
System.out.println("Number = " + a);
// It returns the value obtained by reversing order of the bits in
// the specified int value
System.out.println("By reversing we get = " + Integer.reverse(a));
a = "21";
System.out.println("Number = " + a);
// It returns the value obtained by reversing order of the bits in
// the specified int value
System.out.println("By reversing we get = " + Integer.reverse(a));
}
}
23
1
// Java program to illustrate the
2
// Java.lang.Integer.reverse() method
3
import java.lang.*;
4
5
public class geeks {
6
7
public static void main(String[] args)
8
{
9
10
int a = 37.9;
11
System.out.println("Number = " + a);
12
13
// It returns the value obtained by reversing order of the bits in
14
// the specified int value
15
System.out.println("By reversing we get = " + Integer.reverse(a));
16
a = "21";
17
System.out.println("Number = " + a);
18
19
// It returns the value obtained by reversing order of the bits in
20
// the specified int value
21
System.out.println("By reversing we get = " + Integer.reverse(a));
22
}
23
}
Output:
prog.java:9: error: incompatible types: possible lossy conversion from double to int int a = 37.9; ^ prog.java:17: error: incompatible types: String cannot be converted to int a = "21"; ^ 2 errors