如何在 Java 8 和 Java 9 中使用无符号整数? [英] How to use the unsigned Integer in Java 8 and Java 9?

查看:24
本文介绍了如何在 Java 8 和 Java 9 中使用无符号整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Oracle原始数据类型"中页面,它提到 Java 8 添加了对无符号整数的支持和多头:

In the Oracle "Primitive data types" page, it mentions that Java 8 adds support for unsigned ints and longs:

int:默认情况下,int 数据类型是一个 32 位有符号二进制补码整数,其最小值为 -231 和最大值 231-1.在 Java SE 8 及更高版本中,您可以使用 int 数据类型来表示一个无符号的 32 位整数,其最小值为 0,最大值为 232-1. 使用Integer 类将int 数据类型用作无符号整数.有关更多信息,请参阅数字类部分.compareUnsigneddivideUnsigned 等静态方法已添加到 Integer 类中,以支持无符号整数的算术运算.

int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of −231 and a maximum value of 231−1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232−1. Use the Integer class to use int data type as an unsigned integer. See the section The Number Classes for more information. Static methods like compareUnsigned, divideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers.

long:long 数据类型是一个 64 位二进制补码整数.有符号long的最小值为-263,最大值为263-1.在 Java SE 8 及更高版本中,您可以使用 long 数据类型来表示一个无符号的 64 位 long,其最小值为 0,最大值为值为 264−1. 当您需要比 int 提供的值范围更广的值时,请使用此数据类型.Long 类还包含诸如 compareUnsigneddivideUnsigned 等方法来支持无符号 long 的算术运算.

long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of −263 and a maximum value of 263−1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264−1. Use this data type when you need a range of values wider than those provided by int. The Long class also contains methods like compareUnsigned, divideUnsigned etc to support arithmetic operations for unsigned long.

但是,我找不到声明无符号长整数或整数的方法.例如,以下代码给出了文字超出范围"的编译器错误消息:(当然,我使用的是 Java 8),当它应该在范围内时(分配的值正好是 264−1):

However, I find no way to declare an unsigned long or integer. The following code, for example, gives a compiler error message of "the literal is out of range" (I'm using Java 8, of course), when it should be in range (the value assigned is precisely 264−1):

public class Foo {
    static long values = 18446744073709551615L;
    
    public static void main(String[] args){
        System.out.println(values);
    }  
}

那么,有没有办法声明一个 unsigned int 或 long?

So, is there any way to declare an unsigned int or long?

推荐答案

根据您发布的文档,以及 这篇博文 - 在无符号 int/long 和有符号整数之间声明原语时没有区别.新支持"是在 Integer 和 Long 类中添加静态方法,例如Integer.divideUnsigned.如果您不使用这些方法,则 2^63-1 以上的无符号"long 只是一个带有负值的普通旧 long.

Per the documentation you posted, and this blog post - there's no difference when declaring the primitive between an unsigned int/long and a signed one. The "new support" is the addition of the static methods in the Integer and Long classes, e.g. Integer.divideUnsigned. If you're not using those methods, your "unsigned" long above 2^63-1 is just a plain old long with a negative value.

从快速浏览来看,似乎没有办法在 +/- 2^31-1 或 +/- 2^63-1 以外的范围内声明整数常量.您必须手动计算与超出范围的正值相对应的负值.

From a quick skim, it doesn't look like there's a way to declare integer constants in the range outside of +/- 2^31-1, or +/- 2^63-1 for longs. You would have to manually compute the negative value corresponding to your out-of-range positive value.

这篇关于如何在 Java 8 和 Java 9 中使用无符号整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆