Java 相当于 unsigned long long 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/508630/
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
Java equivalent of unsigned long long?
提问by eleven81
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.
在 C++ 中,我喜欢访问 64 位无符号整数、 viaunsigned long long int或 via uint64_t。现在,我知道在 Java 中 longs 是 64 位。但是,它们已签署。
Is there an unsigned long (long) available as a Java primitive? How do I use it?
是否有 unsigned long (long) 作为 Java 原语可用?我如何使用它?
采纳答案by Sean Bright
I don't believe so. Once you want to go bigger than a signed long, I think BigIntegeris the only (out of the box) way to go.
我不相信。一旦你想变得比有符号多头更大,我认为BigInteger是唯一(开箱即用)的方法。
回答by Paul Tomblin
No, there isn't. The designers of Java are on record as saying they didn't like unsigned ints. Use a BigIntegerinstead. See this questionfor details.
不,没有。Java 的设计者说他们不喜欢无符号整数。请改用BigInteger。有关详细信息,请参阅此问题。
回答by Adam Rosenfield
Nope, there is not. You'll have to use the primitive longdata type and deal with signedness issues, or use a class such as BigInteger.
不,没有。您必须使用原始long数据类型并处理签名问题,或者使用诸如BigInteger.
回答by basszero
Java does not have unsigned types. As already mentioned, incure the overhead of BigInteger or use JNI to access native code.
Java 没有无符号类型。如前所述,承担 BigInteger 的开销或使用 JNI 访问本机代码。
回答by Peter Lawrey
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.
根据您打算执行的操作,结果大致相同,有符号或无符号。但是,除非您使用琐碎的操作,否则您最终将使用 BigInteger。
回答by Mikalai Sabel
Seems like in Java 8 some methods are addedto Long to treat old good [signed] long as unsigned. Seems like a workaround, but may help sometimes.
似乎在 Java 8 中,一些方法被添加到 Long 以将旧的 [signed] long 视为无符号。似乎是一种解决方法,但有时可能会有所帮助。
回答by Andrejs
For unsigned long you can use UnsignedLongclass from Guava library:
对于 unsigned long,您可以使用Guava 库中的UnsignedLong类:
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.
目前似乎缺少的是字节移位运算符。如果您需要这些,您可以使用 Java 中的 BigInteger。
回答by Amr
Starting Java 8, there is support for unsigned long (unsigned 64 bits). The way you can use it is:
从 Java 8 开始,支持 unsigned long(无符号 64 位)。你可以使用它的方式是:
Long l1 = Long.parseUnsignedLong("17916881237904312345");
To print it, you can not simply print l1, but you have to first:
要打印它,您不能简单地打印 l1,但您必须先:
String l1Str = Long.toUnsignedString(l1)
Then
然后
System.out.println(l1Str);
回答by keelar
Java 8provides a set of unsigned long operations that allows you to directly treat those Long variables as unsigned Long, here're some commonly used ones:
Java 8提供了一组 unsigned long 操作,允许您直接将那些 Long 变量视为 unsigned Long,以下是一些常用的:
- String toUnsignedString(long i)
- int compareUnsigned(long x, long y)
- long divideUnsigned(long dividend, long divisor)
- long remainderUnsigned(long dividend, long divisor)
- String toUnsignedString(long i)
- int compareUnsigned(long x, long y)
- 长divideUnsigned(长被除数,除数长)
- 长余数无符号(长被除数,长除数)
And additions, subtractions, and multiplications are the same for signed and unsigned longs.
对于有符号和无符号长整型,加法、减法和乘法是相同的。
回答by user637338
The org.apache.axis.types package has a
org.apache.axis.types 包有一个
UnsignedLong class.
UnsignedLong 类。
for maven:
对于 Maven:
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>

