111

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?

Elrond_EGLDer
  • 47,430
  • 25
  • 189
  • 180
eleven81
  • 7,551
  • 10
  • 34
  • 47
  • 4
    **NOTE** The accepted answer is outdated as of Java 8 and later. See the [Answer by GigaStore](http://stackoverflow.com/a/25248688/642706) for the new feature where you can ask Java to regard a number as unsigned. Not for everyday use, but handy when you need it. – Basil Bourque Sep 27 '15 at 04:17

10 Answers10

144

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);
Amr
  • 2,190
  • 2
  • 12
  • 25
  • @j10, `Long ul1 = Long.parseUnsignedLong(objScannerInstance.next("\\d+"));` Not exactly elegant because it lacks a range check, but it would let you pull in long numeric inputs that would otherwise possible exceed the range of a signed long. _(Leverages the fact that `Scanner::next(...)` can also accept either a Pattern object or String pattern.)_ – Spencer D Jan 17 '18 at 03:41
  • it is difficult. – Alex78191 Dec 06 '18 at 07:00
55

I don't believe so. Once you want to go bigger than a signed long, I think BigInteger is the only (out of the box) way to go.

Sk8erPeter
  • 6,392
  • 9
  • 44
  • 67
Sean Bright
  • 109,632
  • 17
  • 131
  • 138
  • 18
    This answer is a little outdated (it was posted 2009). Starting Java 8 (released March 2014), there is support for unsigned long. Check an example I posted below as an answer. – Amr Jan 19 '15 at 08:44
12

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.

Adam Rosenfield
  • 360,316
  • 93
  • 484
  • 571
7

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.

Community
  • 1
  • 1
Paul Tomblin
  • 167,274
  • 56
  • 305
  • 392
  • 22
    I respect Gosling for what he's done, but I think his defense of no unsigned ints is one of the dumbest excuses I've ever heard. :-) We've got waaaay more wonky things in Java than unsigned ints... :-) – Brian Knoblauch Feb 03 '09 at 20:16
  • 1
    Gosling at JavaPolis 2007 gave an example that confusingly doesn't work for unsigned ints. Josh Bloch pointed out it doesn't work for signed ints either. Arbitrary sized integers ftw! – Tom Hawtin - tackline Feb 03 '09 at 21:20
  • Brian Knoblauch - yep, its created vulnerabile code practice. See [Second "Master Key" Style APK Exploit Is Revealed Just Two Days After Original Goes Public, Already Patched By Google](http://www.androidpolice.com/2013/07/11/second-all-access-apk-exploit-is-revealed-just-two-days-after-master-key-goes-public-already-patched-by-google/). – jww Jul 14 '13 at 05:06
  • 2
    @PP.: I don't think it's possible to define sensible rules which allow free interaction between signed and unsigned types when at least one of them has defined wrapping behavior. That having been said, unsigned byte or unsigned short would have caused *zero* trouble since bytes don't interact with other types anyway. A bigger issue is having defined wrapping behavior for types that are used to represent *numbers*, as distinct from having separate wrapping types for those rare occasions (like hashcode calculations) when wrapping behavior is actually useful. – supercat Feb 25 '14 at 23:52
  • 3
    @PP.: I wish language designers would recognize the importance of distinguishing numbers from algebraic rings (what "wrapping integer" types are). Any size number should implicitly convert to any size ring, but rings should only convert to numbers via function or via explicit typecast to the *same size* number. The behavior of C, where unsigned types generally behave as algebraic rings but sometimes behave as numbers is probably the worst of all possible worlds; I can't fault Gosling for wanting to avoid that, though he took totally the wrong approach for doing so. – supercat Feb 25 '14 at 23:58
6

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.

keelar
  • 5,334
  • 6
  • 35
  • 73
  • 2
    A quick look in the source code tells me to be a bit cautious with these methods. When the longs are indeed negative (i.e. there is a difference from the signed case) the BigInteger class will be used. This means that upto 8 new BigIntegers will be allocated, this is quite a lot and definitely a performance drop. – Toonijn Sep 24 '16 at 18:57
4

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.

Andrejs
  • 24,146
  • 10
  • 98
  • 92
4

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.

Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075
3

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

basszero
  • 28,508
  • 9
  • 50
  • 76
2

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>
user637338
  • 2,080
  • 1
  • 19
  • 22
0

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.