java.lang.Short toUnsignedLong​(short x)

Description

The toUnsignedLong​(short x) method of Short class converts the argument to a long by an unsigned conversion. In an unsigned conversion to a long, the high-order 48 bits of the long are zero and the low-order 16 bits are equal to the bits of the short argument. Consequently, zero and positive short values are mapped to a numerically equal long value and negative short values are mapped to a long value equal to the input plus 216.

Make a note that the toUnsignedLong​​(short x) method of Short class is static thus it should be accessed statically. Mean to say the we would be calling this method in this format:

Short.toUnsignedLong​​(short x)

Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java toUnsignedLong​​(short x)​ method non statically.

Method Syntax

public static long toUnsignedLong​(short x)

Method Argument

Data Type Parameter Description
short x the value to convert to an unsigned int

Method Returns

The toUnsignedLong​(short x) method of Short class returns the argument converted to long by an unsigned conversion

Compatibility

Requires Java 1.8 and up

Java Short toUnsignedLong​​(short x) Example

Below is a simple java example on the usage of toUnsignedLong​​(short x) method of Short class.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

/*
 * This example source code demonstrates the use of  
 * toUnsignedLong​​(short x) method of Short class
 */

public class ShortToUnsignedLongExample {

	public static void main(String[] args) {

		// Ask for user input
		System.out.print("Enter a value:");

		// declare a scanner object to read the user input
		Scanner s = new Scanner(System.in);

		// assign the input to a variable
		short value = s.nextShort();

		// get the result of toUnsignedLong​ method
		long result = Short.toUnsignedLong(value);

		// print the result
		System.out.println("Result:" + result);

		// close the scanner object
		s.close();

	}

}

Sample Output

Below is the sample output when you run the above example.

Java Short toUnsignedLong​(short x) method example output