Java - Short toUnsignedLong() Method



The Java Short toUnsignedLong() method is used to convert the given value to a long by an unsigned conversion.

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

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

Syntax

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

public static long toUnsignedLong(short x)

Parameters

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

Return Value

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

Getting Unsigned long from a Positive short Value Example

We can pass a positive value using toUnsignedLong() method. The result will be the equivalent short converted to long by an unsigned conversion.

package com.tutorialspoint;

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

Output

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

The UnsignedLong value of s is: 98

Getting Unsigned long from a Negative short Value Example

We can also pass a negative value using toUnsignedLong() method. The result will be the equivalent short converted to long 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 UnsignedLong value of the given short value
      System.out.println("The UnsignedLong value of s is: " + Short.toUnsignedLong(s));
   }
}

Output

Following is an output of the above code −

The UnsignedLong value of s is: 65491

Getting Unsigned long 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 long 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 UnsignedLong value of the given short value
      System.out.println("The UnsignedLong value of s is: " + Short.toUnsignedLong(s));
   }
} 

Output

Output of the above code is as follows −

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

Getting Unsigned long 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.toUnsingedLong() 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 UnsignedLong value of the given short value
      System.out.println("The UnsignedLong value of s is: " + Short.toUnsignedLong(s));
   }
} 

Output

While executing the above code we get the following output −

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