Create Unsigned Long in Java

A long
variable in Java is always signed, which always has negative and positive values. But we can also use and treat the general long
variable as an unsigned variable using a few methods mentioned below.
Create Unsigned Long Integer Using BigInteger
in Java
In Java, primitive types like long
provide a vast range of big integer calculations.
Still, to go further than that range, we need to use the BigInteger
class that we can use to perform mathematical operations on larger values than the primitive types can hold. As the unsigned long
value is large, we use the BigInteger
.
In this example, we first call the valueOf()
function of the BigInteger
class and pass Long.MAX_VALUE
, the maximum value it can store. Then, we add the value that we want to store, which we get using BigInteger.valueOf()
and pass the value.
To convert the BigInteger
value to a long
value we use BigInteger.longValue()
and then we make the longValue
as a Unsigned String by calling Long.toUnsignedString()
and pass the longValue
variable. We can see that the value is printed in the output, and it is a positive number which means that it is an unsigned long value.
import java.math.BigInteger;
public class UnsignedLongExample {
public static void main(String[] args) {
BigInteger bigInteger = BigInteger.valueOf(Long.MAX_VALUE)
.add(BigInteger.valueOf(25634));
long longValue = bigInteger.longValue();
String longAsUnsignedString = Long.toUnsignedString(longValue);
System.out.println(longAsUnsignedString);
}
}
Output:
9223372036854801441
If we print the longValue
directly, we get a negative value because long
is still a signed variable but the function toUnsignedString()
converts it to a string with the unsigned value.
System.out.println(longValue);
Output:
-9223372036854750175
Unsigned Long Using ULong
of the jOOU
Another way to get and use an unsigned long
value is by using a third-party library called jOOU
. We need to import the following maven dependency to use it.
<dependency>
<groupId>org.jooq</groupId>
<artifactId>joou</artifactId>
<version>0.9.4</version>
</dependency>
The ULong
class in org.jooq.tools.unsigned
provides a few methods to convert a long
value to an unsigned value. One of the methods is the valueOf()
method that takes a long
value.
In the program, we create a BigInteger
and then in the ULong.valueOf()
method, we pass the long
value using bigInteger.longValue()
that returns a ULong
object.
import org.jooq.tools.unsigned.ULong;
import java.math.BigInteger;
public class UnsignedLongExample {
public static void main(String[] args) {
BigInteger bigInteger = BigInteger.valueOf(Long.MAX_VALUE)
.add(BigInteger.valueOf(25634));
ULong uLong = ULong.valueOf(bigInteger.longValue());
System.out.println(uLong);
}
}
Output:
9223372036854801441
Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.
LinkedInRelated Article - Java Long
- Convert long to double in Java
- Difference Between Long and Int Data Types in Java
- Convert a String to Long in Java