Java中的无符号长

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44413304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:32:35  来源:igfitidea点击:

Unsigned long in Java

javalong-integerbigintegerunsigned

提问by Annapoorni D

Currently, I am using signed values, -2^63 to 2^63-1. Now I need the same range (2 * 2^64), but with positive values only. I found the java documentations mentioning unsigned long, which suits this use.

目前,我使用的是有符号值,-2^63 到 2^63-1。现在我需要相同的范围 (2 * 2^64),但只有正值。我发现 java 文档提到了 unsigned long,它适合这种用途。

I tried to declare 2^64 to a Long wrapper object, but it still loses the data, in other words, it only captures till the Long.MAX_VALUE, so I am clearly missing something. Is BigIntegerthe signed long that Java supports?

我试图将 2^64 声明为 Long 包装器对象,但它仍然会丢失数据,换句话说,它只捕获到Long.MAX_VALUE,所以我显然遗漏了一些东西。是BigInteger的签署久,Java支持?

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

是否有关于如何声明和使用它的定义或指针?

回答by xenteros

In Java 8, unsigned longsupport was introduced. Still, these are typical longs, but the sign doesn't affect adding and subtracting. For dividing and comparing, you have dedicated methods in Long. Also, you can do the following:

在 Java 8 中,unsigned long引入了支持。尽管如此,这些都是典型的多头,但符号不影响加法和减法。对于划分和比较,您在Long. 此外,您可以执行以下操作:

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

BigIntegeris a bit different. It can keep huge numbers. It stores them as int[]and supports arithmetic.

BigInteger有点不同。它可以保留大量数字。它将它们存储为int[]并支持算术。

回答by Stephen C

Java has no unsigned long type, but you can treat signed 64-bit two's-complement integers (i.e. long) as unsigned if you are careful about it.

Java 没有 unsigned long 类型,但是long如果您小心的话,您可以将有符号的 64 位二进制补码整数(即)视为无符号。

Many primitive integer operations are "sign agnostic"; e.g. you can use Java primitive addition, subtraction and multiplication, and get the "right" answer for an unsigned number represented using long.

许多原始整数运算是“符号不可知的”;例如,您可以使用 Java 原始加法、减法和乘法,并为使用long.

For other operations such as division and comparison, the Longclass provides method like divideUnsignedand compareUnsignedthat will give the correct results for unsigned numbers represented as longvalues.

对于其他操作,例如分裂和比较,Long类提供方法等divideUnsigned,并compareUnsigned会给用于表示为无符号数正确的结果long的值。

These methods supporting unsigned operations were added in Java 8. Prior to that, you could use 3rd-party libraries such as Guava; e.g. the static methods in com.google.common.primitives.UnsignedLongs.)

这些支持无符号操作的方法是在Java 8中添加的。在此之前,您可以使用Guava等第三方库;例如com.google.common.primitives.UnsignedLongs.) 中的静态方法

回答by Lukas Eder

If using a third party library is an option, there is jOOU(a spin off library from jOOQ), which offers wrapper types for unsigned integer numbers in Java. That's not exactly the same thing as having primitive type (and thus byte code) support for unsigned types, but perhaps it's still good enough for your use-case.

如果使用第三方库是一种选择,则有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);

All of these types extend java.lang.Numberand can be converted into higher-order primitive types and BigInteger. In your case, earlier versions of jOOU simply stored the unsigned long value in a BigInteger. Version 0.9.3 does some cool bit shifting to fit the value in an ordinary long.

所有这些类型都扩展java.lang.Number并可以转换为高阶原始类型和BigInteger. 在您的情况下,早期版本的 jOOU 只是将 unsigned long 值存储在BigInteger. 0.9.3 版做了一些很酷的位移来适应普通long.

(Disclaimer: I work for the company behind these libraries)

(免责声明:我为这些库背后的公司工作)