Unsigned long in Java

I am currently using signed values, -2 ^ 63 to 2 ^ 63-1. Now I need the same range (2 * 2 ^ 64), but only with positive values. I found that in java docs an unsigned long is mentioned, which is suitable for this use.

I tried to declare a 2 ^ 64 object to a Long wrapper, but it still loses data, in other words, it only captures to Long.MAX_VALUE, so I clearly don't see something. Is a BigIntegerlong subscription supported by Java?

Is there a definition or pointer to how to declare and use it?

+5
source share
3 answers

There is no unsigned long type in Java, but you can consider 64-bit integers with two extra numbers (i.e. long) as unsigned if you are careful with this.

Many primitive integer operations are "signed"; For example, you can use addition, subtraction, and multiplication of Java primitives and get the β€œcorrect” answer for the unsigned number represented by long.

For other operations, such as division and comparison, the class Longare provided such divideUnsignedand compareUnsignedwhich will give correct results for unsigned numbers represented in the form of longvalues.

( Java 8. , Guava; , com.google.common.primitives.UnsignedLongs.)

+6

Java 8 unsigned long unsigned long. , , . Long. :

long l1 = Long.parseUnsignedLong("12345678901234567890");
String l1Str = Long.toUnsignedString(l1)

BigInteger . . int[] .

+4

, jOOU ( jOOQ), Java. , (, , ) , , , .

import static org.joou.Unsigned.*;

// and then...
UByte    b = ubyte(1);
UShort   s = ushort(1);
UInteger i = uint(1);
ULong    l = ulong(1);

java.lang.Number BigInteger. jOOU BigInteger. 0.9.3 , long.

( : )

0
source

Source: https://habr.com/ru/post/1678705/


All Articles