[java] Java equivalent of unsigned long long?

In C++, I enjoyed having access to a 64 bit unsigned integer, via unsigned long long int, or via uint64_t. Now, in Java longs are 64 bits, I know. However, they are signed.

Is there an unsigned long (long) available as a Java primitive? How do I use it?

This question is related to java unsigned primitive unsigned-long-long-int

The answer is


No, there isn't. The designers of Java are on record as saying they didn't like unsigned ints. Use a BigInteger instead. See this question for details.


Java 8 provides a set of unsigned long operations that allows you to directly treat those Long variables as unsigned Long, here're some commonly used ones:

And additions, subtractions, and multiplications are the same for signed and unsigned longs.


Java does not have unsigned types. As already mentioned, incure the overhead of BigInteger or use JNI to access native code.


Nope, there is not. You'll have to use the primitive long data type and deal with signedness issues, or use a class such as BigInteger.


The org.apache.axis.types package has a

UnsignedLong class.

for maven:

<dependency>
    <groupId>org.apache.axis</groupId>
    <artifactId>axis</artifactId>
    <version>1.4</version>
</dependency>

Depending on the operations you intend to perform, the outcome is much the same, signed or unsigned. However, unless you are using trivial operations you will end up using BigInteger.


Nope, there is not. You'll have to use the primitive long data type and deal with signedness issues, or use a class such as BigInteger.


No, there isn't. The designers of Java are on record as saying they didn't like unsigned ints. Use a BigInteger instead. See this question for details.


Depending on the operations you intend to perform, the outcome is much the same, signed or unsigned. However, unless you are using trivial operations you will end up using BigInteger.


Seems like in Java 8 some methods are added to Long to treat old good [signed] long as unsigned. Seems like a workaround, but may help sometimes.


Starting Java 8, there is support for unsigned long (unsigned 64 bits). The way you can use it is:

Long l1 = Long.parseUnsignedLong("17916881237904312345");

To print it, you can not simply print l1, but you have to first:

String l1Str = Long.toUnsignedString(l1)

Then

System.out.println(l1Str);

Java does not have unsigned types. As already mentioned, incure the overhead of BigInteger or use JNI to access native code.


No, there isn't. The designers of Java are on record as saying they didn't like unsigned ints. Use a BigInteger instead. See this question for details.


The org.apache.axis.types package has a

UnsignedLong class.

for maven:

<dependency>
    <groupId>org.apache.axis</groupId>
    <artifactId>axis</artifactId>
    <version>1.4</version>
</dependency>

Nope, there is not. You'll have to use the primitive long data type and deal with signedness issues, or use a class such as BigInteger.


Depending on the operations you intend to perform, the outcome is much the same, signed or unsigned. However, unless you are using trivial operations you will end up using BigInteger.


For unsigned long you can use UnsignedLong class from Guava library:

It supports various operations:

  • plus
  • minus
  • times
  • mod
  • dividedBy

The thing that seems missing at the moment are byte shift operators. If you need those you can use BigInteger from Java.


No, there isn't. The designers of Java are on record as saying they didn't like unsigned ints. Use a BigInteger instead. See this question for details.


Nope, there is not. You'll have to use the primitive long data type and deal with signedness issues, or use a class such as BigInteger.


Java 8 provides a set of unsigned long operations that allows you to directly treat those Long variables as unsigned Long, here're some commonly used ones:

And additions, subtractions, and multiplications are the same for signed and unsigned longs.


Starting Java 8, there is support for unsigned long (unsigned 64 bits). The way you can use it is:

Long l1 = Long.parseUnsignedLong("17916881237904312345");

To print it, you can not simply print l1, but you have to first:

String l1Str = Long.toUnsignedString(l1)

Then

System.out.println(l1Str);

Seems like in Java 8 some methods are added to Long to treat old good [signed] long as unsigned. Seems like a workaround, but may help sometimes.


Java does not have unsigned types. As already mentioned, incure the overhead of BigInteger or use JNI to access native code.


Depending on the operations you intend to perform, the outcome is much the same, signed or unsigned. However, unless you are using trivial operations you will end up using BigInteger.