Convert to a number by an unsigned conversion in Java

 Write a Java program that converts the given int to a long by an unsigned conversion.

Java program to convert to a number by an unsigned conversion

public class Main {

    public static void main(String[] args) {

        long result = Integer.toUnsignedLong(Integer.MIN_VALUE);

        System.out.println("Integer.MIN_VALUE: " + Integer.MIN_VALUE);
        System.out.println("Result: " + result);

        System.out.println();
                
        int result1 = Short.toUnsignedInt(Short.MIN_VALUE);
        int result2 = Short.toUnsignedInt(Short.MAX_VALUE);
        
        System.out.println("Short.MIN_VALUE: " + Short.MIN_VALUE + " Short.MAX_VALUE: " + Short.MAX_VALUE);
        System.out.println("Result 1: " + result1);
        System.out.println("Result 2: " + result2);
    }

}

Output:

Integer.MIN_VALUE: -2147483648
Result: 2147483648

Short.MIN_VALUE: -32768 Short.MAX_VALUE: 32767
Result 1: 32768
Result 2: 32767

Comments