The **toUnsignedLong()** method is a static method of the **Integer wrapper class** which is used to convert an int primitive to long by using the unsigned conversion. In this post, we are going to look at the **toUnsignedLong()** method in detail.

**Method declaration –**public static long toUnsignedLong(int x)**What does it do?**It will convert the integer passed in the argument to a long value by using the unsigned conversion. We will look at how both positive and negative integers will be converted to their corresponding unsigned long value**What does it return?**It will return a long value after the conversion

**Now, what is signed and unsigned long values? **Signed long contain both positive and negative numbers and their range is from [-2^{63} to 2^{63}-1] whereas unsigned long contains only non-negative numbers ranging from [0 to 2^{64}-1].

**Converting the positive integers using the toUnsignedLong() method**

Zero or positive int values are mapped to a numerically equal long value.

```
public class Codekru {
public static void main(String[] args) {
int i = 12;
System.out.println("Unsigned long value: " + Integer.toUnsignedLong(i));
}
}
```

**Output –**

```
Unsigned long value: 12
```

**Converting the negative integers using the toUnsignedLong() method**

Negative int values will be converted to unsigned long by adding 2^{32} to the input value. So, if the int value is -12, then the unsigned long value would be -12+2^{32}.

```
public class Codekru {
public static void main(String[] args) {
int i = -12;
System.out.println("2^32 = " + (long) Math.pow(2, 32));
// -12+2^32 = -12 + 4294967296 = 4294967284s
System.out.println("Unsigned long value: " + Integer.toUnsignedLong(i));
}
}
```

**Output –**

```
2^32 = 4294967296
Unsigned long value: 4294967284
```

**Q- What if we passed an Integer instance instead of the int primitive into the function’s argument?**

Here, the integer instance will be unboxed to its primitive value, and then it would be converted to an unsigned long.

```
public class Codekru {
public static void main(String[] args) {
// using valueOf method to create integer instance
Integer i = Integer.valueOf(12);
System.out.println("Unsigned long value: " + Integer.toUnsignedLong(i));
}
}
```

**Output –**

`Unsigned long value: 12`

Please visit **this link** if you want to know more about the **Integer wrapper** class of java and its other functions or methods.

Hope you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected].