Signup/Sign In
LAST UPDATED: NOVEMBER 5, 2020

Java Integer toUnsignedLong() Method

Java toUnsignedLong() method is a part of the Integer class of the java.lang package. This method is used to return the long equivalent of the integer value by unsigned conversion.

In the case of unsigned conversion to long the lower order 32 bits are taken as the actual bits of the integer value passed while the higher-order 32 bits are considered as zero.

Syntax:

public static long toUnsignedLong(int i)  

Parameters:

The parameter passed is the integer value whose long equivalent is to be returned using the unsigned conversion.

Returns:

Returns the long equivalent of the integer value passed as an argument using the unsigned conversion.

Example 1:

Here, some signed and unsigned integer values are converted into its long equivalent using unsigned conversion.

public class StudyTonight
{  
    public static void main(String[] args) 
    {  
        int a = 10;  
        int b = 20;  
        int c = -10;  
        int d = -20;  
         
        long a1 = Integer.toUnsignedLong(a);  //unsigned conversion to long 
        long b1 = Integer.toUnsignedLong(b);  //unsigned conversion to long 
        long c1 = Integer.toUnsignedLong(c);  //unsigned conversion to long   
        long d1 = Integer.toUnsignedLong(d);  //unsigned conversion to long       
        System.out.println("Long Equivalent is  = " + a1);  
        System.out.println("Long Equivalent is  = " + b1);  
        System.out.println("Long Equivalent is  = " + c1);          
        System.out.println("Long equivalent is  = " + d1);  
    }  
}  


Long Equivalent is = 10
Long Equivalent is = 20
Long Equivalent is = 4294967286
Long equivalent is = 4294967276

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.util.Scanner; 

public class StudyTonight
{  
    public static void main(String[] args) 
    {            
        try
        {
           System.out.print("Enter the value ");  
           Scanner sc = new Scanner(System.in);  
           int value = sc.nextInt();                                  
           long result = Integer.toUnsignedLong(value);   //returns long equivalent               
           System.out.println("Output: "+result); 
        }
        catch(Exception e)
        {
           System.out.println("Invalid Input!!");
        }        
    }  
}  


Enter the value 93
Output: 93
**********************
Enter the value -18
Output: 4294967278
**********************
Enter the value 0x590
Invalid Input!!

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.