在java中处理无符号数据的公认方法是什么

发布于 2024-12-15 00:52:53 字数 94 浏览 5 评论 0原文

我读到(并且目前正在使用这个)只要我需要一个无符号整数,我就可以使用 long 。这对我来说效果很好,但感觉设计很糟糕。有没有更好、更容易被接受的方法呢?那么无符号长整数呢?

I read (and am currently using this) that I can use a long whenever I need an unsigned int. This is working fine for me, but feels like a poor design. Is there a better, more accepted, way to do it? What about unsigned longs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

翻了热茶 2024-12-22 00:52:53

由于 Java 中唯一的无符号基元类型是 char,它具有非常特定的含义来表示 unicode 字符,因此使用 long 是最惯用的方法。

默认情况下,JAXB 也将 xsd:unsignedInt 映射到 long。 (来源

编辑:如果您知道最上面的一点为 0,或者仅使用符号无关紧要的操作(例如,您只是传递数据),int 可能更常见。 JAXB 仅默认为 long,因为它无法知道您打算如何使用数据。

Since the only unsigned primitive type in Java is char, which has a very specific meaning to denote a unicode character, using long is the most idiomatic way to go.

By default, JAXB maps xsd:unsignedInt to long, too. (source)

Edit: If you know the top bit to be 0, or only use operations where the sign doesn't matter (for instance, you're just passing data through), int is probably more common. JAXB only defaults to long because it can't know how you intend to use the data.

心作怪 2024-12-22 00:52:53

Java 没有选择无符号类型,您使用比原始无符号类型大的有符号类型来解决限制,但因此您使用了所需内存的两倍。

在java中没有无符号的概念。

Java doesn't have unsigned types by choice, you are using the signed types that are larger than the original unsigned type to work around the limitation but so you are using twice of the needed memory.

In java there is no concept of unsigned .

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