Java - Short toUnsignedInt() Method



The Java Short toUnsignedInt() method is used to convert the given value to an int by an unsigned conversion.

The low-order 16 bits of the int are equivalent to the bits of the given short valuewhile the high-order 16 bits of the int are zero in an unsigned conversion to an int.

This results into mapping of the negative short values to an int value equal to the input plus 216, while zero and positive short values are mapped to an int value that is numerically equal.

Syntax

Following is the syntax for Java Short toUnsignedInt() method −

public static int toUnsignedInt(short x)

Parameters

  • x − This is the short to be converted to unsigned int.

Return Value

This method returns the short which is converted to int by an unsigned conversion.

Getting Unsigned Int from a Positive short Value Example

We can pass a positive value using toUnsignedInt() method. The result will be the equivalent short converted to int by an unsigned conversion. Following is an example to show the usage of Java Short toUnsignedInt() method.

package com.tutorialspoint; public class ShortDemo { public static void main(String[] args) { // create short value short s = 98; // print UnsignedInt value of the given short value System.out.println("The UnsignedInt value of s is: " + Short.toUnsignedInt(s)); } }

Output

Let us compile and run the above program, this will produce the following result −

The UnsignedInt value of s is: 98

Getting Unsigned Int from a Negative short Value Example

We can also pass a negative value using the toUnsignedInt() method. The result will be the equivalent short converted to int by an unsigned conversion which is positive.

package com.tutorialspoint; public class ShortDemo { public static void main(String[] args) { // create short value short s = -45; // print UnsignedInt value of the given short value System.out.println("The UnsignedInt value of s is: " + Short.toUnsignedInt(s)); } }

Output

Following is an output of the above code −

The UnsignedInt value of s is: 65491

Getting Unsigned Int from a Maximum short Value Example

If you pass MAX_VALUE as an argument to this method, it returns the value 32767.

In the example below short variable s is created. Then the MAX_VALUE is assigned as an value to this variable. Thereafter, we retrieve its corresponding short value converted to int by an unsigned conversion −

package com.tutorialspoint; public class ShortDemo { public static void main(String[] args) { // create short value short s = Short.MAX_VALUE; System.out.println("The short MAX_VALUE is: " + s); // print UnsignedInt value of the given short value System.out.println("The UnsignedInt value of s is: " + Short.toUnsignedInt(s)); } }

Output

Output of the above code is as follows −

The short MAX_VALUE is: 32767
The UnsignedInt value of s is: 32767

Getting Unsigned Int from a Minimum short Value Example

If you pass MIN_VALUE as an argument to this method, it returns the value 32768.

In the example below short variable s is created. Then the MIN_VALUE is assigned as an value to this variable. Then the unsigned value of the given short value is returned by short.toUnsingedInt() method −

package com.tutorialspoint; public class ShortDemo { public static void main(String[] args) { // create short value short s = Short.MIN_VALUE; System.out.println("The short MIN_VALUE is: " + s); // print UnsignedInt value of the given short value System.out.println("The UnsignedInt value of s is: " + Short.toUnsignedInt(s)); } }

Output

While executing the above code we get the following output −

The short MIN_VALUE is: -32768
The UnsignedInt value of s is: 32768
java_lang_short.htm
Advertisements