Java相当于unsigned long long?

发布于 2024-07-13 07:26:55 字数 196 浏览 9 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

聽兲甴掵 2024-07-20 07:26:55

从 Java 8 开始,支持 unsigned long(无符号 64 位)。 你可以使用它的方式是:

Long l1 = Long.parseUnsignedLong("17916881237904312345");

要打印它,你不能简单地打印 l1,但你必须首先:

String l1Str = Long.toUnsignedString(l1)

然后

System.out.println(l1Str);

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);
谈场末日恋爱 2024-07-20 07:26:55

我不相信是这样。 一旦你想要大于有符号长整型,我认为 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.

双手揣兜 2024-07-20 07:26:55

不,没有。 您必须使用原始 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 as BigInteger.

酒解孤独 2024-07-20 07:26:55

不,没有。 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.

彼岸花似海 2024-07-20 07:26:55

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.

土豪 2024-07-20 07:26:55

根据您打算执行的操作,结果大致相同,有符号或无符号。 但是,除非您使用简单的操作,否则您最终将使用 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.

故事与诗 2024-07-20 07:26:55

对于无符号长整型,您可以使用 UnsignedLong 的 a> 类Guava 库

它支持各种操作:

  • 加减
  • 除以

目前似乎缺少的是字节移位运算符 如果您需要这些,您可以使用 Java 中的 BigInteger。

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.

莳間冲淡了誓言ζ 2024-07-20 07:26:55

Java 没有无符号类型。 正如已经提到的,增加 BigInteger 的开销或使用 JNI 访问本机代码。

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

歌枕肩 2024-07-20 07:26:55

org.apache.axis.types 包有一个

UnsignedLong 类。

对于行家:

<dependency>
    <groupId>org.apache.axis</groupId>
    <artifactId>axis</artifactId>
    <version>1.4</version>
</dependency>

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>
初吻给了烟 2024-07-20 07:26:55

似乎在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文