为什么Java能够将0xff000000存储为int?

发布于 2024-07-09 06:54:49 字数 185 浏览 3 评论 0原文

Java 中整数的最大值是 2147483647,因为 Java 整数是有符号的,对吧?

0xff000000 的数值为 4278190080。

但是我看到这样的 Java 代码:

int ALPHA_MASK = 0xff000000;

谁能启发我?

An integer's max value in Java is 2147483647, since Java integers are signed, right?

0xff000000 has a numeric value of 4278190080.

Yet I see Java code like this:

int ALPHA_MASK = 0xff000000;

Can anyone enlighten me please?

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

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

发布评论

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

评论(5

埋情葬爱 2024-07-16 06:54:49

只是对埃里克森的答案的补充:

正如他所说,在大多数计算机体系结构上,有符号整数存储为各自正值的补码。

也就是说,全部 2^32 个可能的值被分为两组:一组代表从 0 位开始的正值,一组代表从 1 开始的负值。

现在,想象一下我们仅限于 3 位数字。 让我们以一种有趣的方式排列它们,很快就会有意义:

     000
  111   001 
110       010
  101   011  
     100  

您会看到左侧的所有数字都以 1 位开头,而右侧的所有数字都以 0 开头。根据我们之前的决定为了将前者声明为负数,将后者声明为正数,我们看到 001、010 和 011 是唯一可能的正数,而 111、110 和 101 则是它们各自的负数。

现在我们分别如何处理顶部和底部的两个数字? 显然,000 应该为零,而 100 将是所有没有正数对应项的最小负数。 总结一下:

     000      (0)
  111   001   (-1 / 1)
110       010 (-2 / 2)
  101   011   (-3 / 3)
     100      (-4)

您可能会注意到,可以通过对 1 (001) 求反并添加 1 (001) 来获得 -1 (111) 的位模式:
001(= 1)-> 110+001-> 111 (= -1)

回到你的问题:

0xff000000 = 1111 1111 0000 0000 0000 0000 0000 0000

我们不必在它前面添加更多的零,因为我们已经达到了 32 位的最大值。
另外,它显然是一个负数(因为它以 1 位开始),所以我们现在要计算它的绝对值/正对应值:

这意味着,我们将取其二进制补码,

1111 1111 0000 0000 0000 0000 0000 0000

然后

0000 0000 1111 1111 1111 1111 1111 1111

我们添加

0000 0000 0000 0000 0000 0000 0000 0001

和因此

0000 0001 0000 0000 0000 0000 0000 0000 = 16777216

,0xff000000 = -16777216。

Just an addition to erickson's answer:

As he said, signed integers are stored as two's complements to their respective positive value on most computer architectures.

That is, the whole 2^32 possible values are split up into two sets: one for positive values starting with a 0-bit and one for negative values starting with a 1.

Now, imagine that we're limited to 3-bit numbers. Let's arrange them in a funny way that'll make sense in a second:

     000
  111   001 
110       010
  101   011  
     100  

You see that all numbers on the left-hand side start with a 1-bit whereas on the right-hand side they start with a 0. By our earlier decision to declare the former as negative and the latter as positive, we see that 001, 010 and 011 are the only possible positive numbers whereas 111, 110 and 101 are their respective negative counterparts.

Now what do we do with the two numbers that are at the top and the bottom, respectively? 000 should be zero, obviously, and 100 will be the lowest negative number of all which doesn't have a positive counterpart. To summarize:

     000      (0)
  111   001   (-1 / 1)
110       010 (-2 / 2)
  101   011   (-3 / 3)
     100      (-4)

You might notice that you can get the bit pattern of -1 (111) by negating 1 (001) and adding 1 (001) to it:
001 (= 1) -> 110 + 001 -> 111 (= -1)

Coming back to your question:

0xff000000 = 1111 1111 0000 0000 0000 0000 0000 0000

We don't have to add further zeros in front of it as we already reached the maximum of 32 bits.
Also, it's obviously a negative number (as it's starting with a 1-bit), so we're now going to calculate its absolute value / positive counterpart:

This means, we'll take the two's complement of

1111 1111 0000 0000 0000 0000 0000 0000

which is

0000 0000 1111 1111 1111 1111 1111 1111

Then we add

0000 0000 0000 0000 0000 0000 0000 0001

and obtain

0000 0001 0000 0000 0000 0000 0000 0000 = 16777216

Therefore, 0xff000000 = -16777216.

甜警司 2024-07-16 06:54:49

高位是符号位。 设置为负数:-16777216。

与大多数语言一样,Java 以 2 的补码 形式存储带符号的数字。 在本例中,从 0x7F000000(即 2130706432)减去 231(即 2147483648)将得到 -16777216。

The high bit is a sign bit. Setting it denotes a negative number: -16777216.

Java, like most languages, stores signed numbers in 2's complement form. In this case, subtracting 231, or 2147483648 from 0x7F000000, or 2130706432, yields -16777216.

怼怹恏 2024-07-16 06:54:49

可能值得指出的事情 - 该代码并不意味着用作具有数值的整数; 目的是作为位掩码从 32 位颜色值中过滤掉 Alpha 通道。 这个变量实际上甚至不应该被视为一个数字,而只是一个高 8 位打开的二进制掩码。

Something probably worth pointing out - this code is not meant to be used as an integer with a numerical value; The purpose is as a bitmask to filter the alpha channel out of a 32 bit color value. This variable really shouldn't even be thought of as a number, just as a binary mask with the high 8 bits turned on.

怎樣才叫好 2024-07-16 06:54:49

额外的位用于符号

Java ints are 二进制补码

the extra bit is for the sign

Java ints are twos complement

情绪少女 2024-07-16 06:54:49

Java 中的 int 是有符号的。

ints are signed in Java.

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