[英]Value changing when converting short to int in C
I've been experimenting with simple code and I noticed:我一直在尝试简单的代码,我注意到:
short x = 0x8765;
int y = (int)x;
printf("y = %d\n",y);
would print out "y = -30875".将打印出“y = -30875”。 I'm wondering why is it the case since when I convert 0x8765 from hex into decimal I got y = 34661.我想知道为什么会这样,因为当我将 0x8765 从十六进制转换为十进制时,我得到了 y = 34661。
The bit pattern of 0x8765
is negative in a 16-bit signed two's complement integer, but positive in a 32-bit signed two's complement integer. 0x8765 的位模式在 16 位有符号二进制补码0x8765
中为负,但在 32 位有符号二进制补码 integer 中为正。
In an int16_t
:在int16_t
:
0b1000011101100101
//^ sign bit set
In an int32_t
:在int32_t
:
0b00000000000000001000011101100101
//^ sign bit unset
The range of an 4-bytes integer datatype with short
type qualifier in C is considered to hold the values from -32,768 to 32,767. C 中具有short
类型限定符的 4 字节 integer 数据类型的范围被认为包含从 -32,768 到 32,767 的值。 But the way you're trying to hold the integer 34661 in the short int
is incorrect;但是您尝试将 integer 34661 保存在short int
中的方式不正确; the value couldn't be held by it.价值不能被它持有。
Another point, the conversion is clearly correct, since the short int
variable x
is being overflowed, a negative integer, is then assigned to x
and it's explicitly did typecast to int
variable y
and assigned the value of x
in it.另一点,转换显然是正确的,因为short int
变量x
被溢出,然后将负 integer 分配给x
,并且它显式地对int
变量y
进行了类型转换,并在其中分配了x
的值。
In other words, the maximum value which could be held by short int
type in hex: 0x7FFF
(ie 32767).换句话说, short int
类型可以保持的最大值(十六进制): 0x7FFF
(即 32767)。
Note: You may use unsigned short int
to extend the capacity of short
from 0 to 65535 (since it's unsigned, the value must not be the negative integer):注意:您可以使用unsigned short int
将short
的容量从 0 扩展到 65535(因为它是无符号的,所以该值不能是负整数):
|(-32768) + 32767| = -(-32768) + 32767 = 32768 + 32767 => 65535
If you try to assign the number 0x8765
( 34661
decimal) to a short int
you are assigning an out of range value to it.如果您尝试将数字0x8765
(十进制34661
)分配给一个short int
,则您正在为其分配一个超出范围的值。 The range of short
goes from -32768
to +32767
and the literal value in decimal for 0x8765
is +34661
which is clearly over the highest value allowed for a short
. short
的范围从-32768
到+34661
+32767
0x8765
显然超过了short
允许的最大值。 So you are incurring on Undefined Behaviour.所以你正在招致未定义的行为。
What happens is not specified by the standard.标准没有规定会发生什么。
It is the same problem if you try to assign 100000000000
to a char
variable.如果您尝试将100000000000
分配给char
变量,则会出现同样的问题。 As you have not checked the value in x
you thought that the value changed at some point.由于您尚未检查x
中的值,因此您认为该值在某个时候发生了变化。 But it didn't.但事实并非如此。 For some unexplain reason that Undefined Behaviour resulted in -30875
being assigned to x
.由于某些无法解释的原因,未定义的行为导致-30875
被分配给x
。 But also an error could be raised by the runtime, and your program stopped, or even more strange things.但运行时也可能引发错误,您的程序停止,甚至更奇怪的事情。 That is what UB means.这就是UB的意思。 Probably if you had printed the values of x
and y
you could observe the same value on both, demonstrating that the problem came before.可能如果您打印了x
和y
的值,您可以在两者上观察到相同的值,这表明问题出现在之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.