Java相当于unsigned long long?
在 C++ 中,我喜欢通过 unsigned long long int
或通过 uint64_t
访问 64 位无符号整数。 现在,我知道 Java 中的 long 是 64 位。 然而,他们已经签署了。
是否有 unsigned long(长整型)可用作 Java 原语? 我该如何使用它?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
从 Java 8 开始,支持 unsigned long(无符号 64 位)。 你可以使用它的方式是:
要打印它,你不能简单地打印 l1,但你必须首先:
然后
Starting Java 8, there is support for unsigned long (unsigned 64 bits). The way you can use it is:
To print it, you can not simply print l1, but you have to first:
Then
我不相信是这样。 一旦你想要大于有符号长整型,我认为 BigInteger 是唯一的(开箱即用的)方法。
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.
不,没有。 您必须使用原始
long
数据类型并处理符号问题,或者使用诸如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 asBigInteger
.不,没有。 Java 的设计者曾公开表示他们不喜欢无符号整数。 请改用 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.
Java 8 提供了一组无符号长整型操作允许您直接将这些 Long 变量视为 unsigned Long,这里有一些常用的:
以及加法、减法和对于有符号长整型和无符号长整型,乘法是相同的。
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.
根据您打算执行的操作,结果大致相同,有符号或无符号。 但是,除非您使用简单的操作,否则您最终将使用 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.
对于无符号长整型,您可以使用 UnsignedLong 的 a> 类Guava 库:
它支持各种操作:
目前似乎缺少的是字节移位运算符 如果您需要这些,您可以使用 Java 中的 BigInteger。
For unsigned long you can use UnsignedLong class from Guava library:
It supports various operations:
The thing that seems missing at the moment are byte shift operators. If you need those you can use BigInteger from Java.
Java 没有无符号类型。 正如已经提到的,增加 BigInteger 的开销或使用 JNI 访问本机代码。
Java does not have unsigned types. As already mentioned, incure the overhead of BigInteger or use JNI to access native code.
org.apache.axis.types 包有一个
UnsignedLong 类。
对于行家:
The org.apache.axis.types package has a
UnsignedLong class.
for maven:
似乎在 Java 8 中一些方法添加到 Long 来处理旧的好[签名]长如未签名。 似乎是一种解决方法,但有时可能会有所帮助。
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.