为什么 Java 整数中的整数不使用所有 32 或 64 位?

发布于 2024-12-19 06:39:48 字数 235 浏览 1 评论 0原文

我正在研究 32 位和 64 位。我注意到可以存储在 32 位中的整数值的范围是 ±4,294,967,295 但 Java int 也是 32 位(如果我没记错的话)并且它存储值高达 ±2 147 483 648long 也是如此,它存储从 0 到 ±2^63 的值,但 64 位存储 ±2^64 值。为什么这些值不同?

I was looking into 32-bit and 64-bit. I noticed that the range of integer values that can stored in 32 bits is ±4,294,967,295 but the Java int is also 32-bit (If I am not mistaken) and it stores values up to ±2 147 483 648. Same thing for long, it stores values from 0 to ±2^63 but 64-bit stores ±2^64 values. How come these values are different?

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

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

发布评论

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

评论(3

嘿咻 2024-12-26 06:39:48

Java 中的整数是有符号的,因此保留一位来表示该数字是正数还是负数。这种表示法称为“二进制补码表示法”。 通过这种方法,最大正值表示为n 位由

(2 ^ (n - 1)) - 1

给出,相应的最小负值由

-(2 ^ (n EM>- 1))

正负界限的“相差一”方面是由于零。零占据一个槽位,留下偶数个负数和奇数个正数。如果将表示的值想象为圆圈上的标记(就像钟面上的小时),您会发现零更属于正范围而不是负范围。换句话说,如果将零视为正数,您会发现正值和负值范围具有更多对称性。

要学习这种表示,请从小处开始。比如说,取三位并写出所有可以表示的数字:

  • 0
  • 1
  • 2
  • 3
  • -4
  • -3
  • -2
  • -1

你能写出定义每个数字的三位序列吗?一旦您了解了如何做到这一点,请再尝试一下。从这里开始,您可以想象它如何扩展到 32 或 64 位。

该序列形成一个“轮子”,其中每个轮子都是通过在前一个轮子上加 1 来形成的,并注明从 3 到 -4 的环绕。这种环绕效果(减法也可能发生)称为“模算术”

Integers in Java are signed, so one bit is reserved to represent whether the number is positive or negative. The representation is called "two's complement notation." With this approach, the maximum positive value represented by n bits is given by

(2 ^ (n - 1)) - 1

and the corresponding minimum negative value is given by

-(2 ^ (n - 1))

The "off-by-one" aspect to the positive and negative bounds is due to zero. Zero takes up a slot, leaving an even number of negative numbers and an odd number of positive numbers. If you picture the represented values as marks on a circle—like hours on a clock face—you'll see that zero belongs more to the positive range than the negative range. In other words, if you count zero as sort of positive, you'll find more symmetry in the positive and negative value ranges.

To learn this representation, start small. Take, say, three bits and write out all the numbers that can be represented:

  • 0
  • 1
  • 2
  • 3
  • -4
  • -3
  • -2
  • -1

Can you write the three-bit sequence that defines each of those numbers? Once you understand how to do that, try it with one more bit. From there, you imagine how it extends up to 32 or 64 bits.

That sequence forms a "wheel," where each is formed by adding one to the previous, with noted wraparound from 3 to -4. That wraparound effect (which can also occur with subtraction) is called "modulo arithemetic."

ゃ人海孤独症 2024-12-26 06:39:48

在 32 位中,您可以存储 2^32 个值。如果您将这些值称为 0 到 4294967295 或 -2147483648 到 +2147483647,则由您决定。这种差异称为“有符号类型”与“无符号类型”。 Java 语言仅支持 int 有符号类型。其他语言对于无符号 32 位类型有不同的类型。

NO 语言将具有 ±4294967295 的 32 位类型,因为“-”部分需要另一位。

In 32 bit you can store 2^32 values. If you call these values 0 to 4294967295 or -2147483648 to +2147483647 is up to you. This difference is called "signed type" versus "unsigned type". The language Java supports only signed types for int. Other languages have different types for an unsigned 32bit type.

NO laguage will have a 32bit type for ±4294967295, because the "-" part would require another bit.

倚栏听风 2024-12-26 06:39:48

这是因为 Java int 是有符号的,因此您需要一位来表示符号。

That's because Java ints are signed, so you need one bit for the sign.

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