Write the example program
Write the following code block to implement example program:
import java.lang.Integer;
public class IntegerReverse {
public static void main(String[] args) {
int positiveValue = 202;
int negativeValue = -50;
System.out.println("Original Positive Number:" + positiveValue);
System.out.println("Binary Representation:" + Integer.toBinaryString(positiveValue));
System.out.println("Number after reversal:" + Integer.reverse(positiveValue));
System.out.println("\nOriginal Negative Number:" + negativeValue);
System.out.println("Binary Representation:" + Integer.toBinaryString(negativeValue));
System.out.println("Number after reversal:" + Integer.reverse(negativeValue));
}
}
In this step, we have learned to import the java.lang.Integer
package into our class. We have defined two integer variables and assigned them with values.
Using the toBinaryString()
method, we have converted these values into their binary numbers by its calling inside the System.out.println()
method. Then we have called the reverse()
method on these two variables to get their reversed binary representation.