简体   繁体   English

当一个长整数被转换成一个短整数时,会发生什么?

[英]When a long integer is cast into a short one, what happened?

I use java to copy one long integer y to a short integer x: 我使用java将一个长整数y复制到一个短整数x:

long y = 40002;
short x = (short) y;
System.out.println("x now equals " + x);

The result is: x now equals -25534. 结果是:x现在等于-25534。

I tried to figure out how 40002 was cast into -25534, but I failed. 我试图弄清楚40002是如何投入-25534的,但我失败了。 The 40002 corresponds to 1001 1100 0100 0010, the -25534 corresponds to 1110 0011 1011 1110. Can any friend tell me what happened in this process? 40002对应1001 1100 0100 0010,-25534对应1110 0011 1011 1110.任何朋友都可以告诉我这个过程中发生了什么? Thanks a lot! 非常感谢!

What you have done by casting a long to a short is a narrowing primitive conversion , which is covered by the JLS, Section 5.1.3 : 你通过将一个longshort方法所做的是一个缩小的原始转换 ,这由JLS第5.1.3节涵盖:

A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value. 将有符号整数缩小到整数类型T只会丢弃除n个最低位之外的所有位,其中n是用于表示类型T的位数。除了可能丢失有关数值大小的信息之外,这可能导致结果值的符号与输入值的符号不同。

The long value 40002 is the following 64 bits: long40002是以下64位:

00000000 00000000 00000000 00000000 00000000 00000000 10011100 01000010

The conversion only retains the least significant 16 bits: 转换仅保留最低16位:

10011100 01000010

That leading 1 is interpreted in 2's complement notation to be -2^15, not +2^15. 前导1以2的补码表示法解释为-2 ^ 15,而不是+ 2 ^ 15。 That explains why there is a difference of 2^16, of 65,536, in the long value and the short value. 这就解释了为什么在long值和short值中存在2 ^ 16,65,536的差异。

Integer overflow happened. 整数溢出发生了。

A short is two signed bytes, which means that Short.MAX_VALUE is 2 15 -1, which is 32,767. short是两个带符号的字节,这意味着Short.MAX_VALUE是2 15 -1,即32,767。 "Larger" values logically "wrap around" into the negative range. “较大”值在逻辑上“环绕”到负范围内。

In this case the excess amount is 40,002 - 2 15 = 7234 在这种情况下,过量是40,002 - 2 15 = 7234
Short.MIN_VALUE is -2 15 = -32,768 Short.MIN_VALUE是-2 15 = -32,768
-32,768 + 7234 = -25,534 -32,768 + 7234 = -25,534

which is the number you're wondering about. 这是你想知道的数字。

Thank all of you guys. 谢谢你们所有人。 According to all of your answers, I summarize as follows: The long value 40002 is the following 64 bits: 根据你的所有答案,我总结如下:长值40002是以下64位:

00000000 00000000 00000000 00000000 00000000 00000000 10011100 01000010

The conversion only retains the least significant 16 bits: 转换仅保留最低16位:

10011100 01000010

When JVM regard 10011100 01000010 as a short integer, it will compute it like this: 当JVM将10011100 01000010视为一个短整数时,它会像这样计算:

-2^15 + 00011100 01000010 = -32768 + 7234 = -25534

This is it. 就是这个。

Basically, it's going to cycle through the values, when you reach the max and add 1 it's going to be the lowest value, so 32768 is going to be -32768, when you reach 65536 (32768*2) it's going to be 0 and when you reach 98303 (32768*2+32767) it's going to be 32767, if you add one you'll get to 98304 (32768*3) and it's going to be -32768 again. 基本上,它会循环显示值,当你达到最大值并加1它将是最低值,所以32768将是-32768,当你达到65536(32768 * 2)它将是0并且当你达到98303(32768 * 2 + 32767)它将是32767,如果你加一个你将得到98304(32768 * 3)并且它将再次是-32768。

So 40002 (which is higher than 32768 but lower than 32768*2) is clearly going to be a negative number when converted to short. 所以40002(高于32768但低于32768 * 2)在转换为空头时显然是负数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM