简体   繁体   English

short int整数换行/ c中的short int反转不了解,赋值和打印之间的差异

[英]short int Integer wrap around / short int inversion in c not understood, difference between assignment and prints

The following code fragment 以下代码片段

short int k = -32768;
printf("%d \n", -k);
k=-k;
printf("%d \n", k);

prints 版画

32768 
-32768

I would assume that both prints are equal. 我假设两个打印都是一样的。 Can somebody explain what the difference is and why the assignment k=-k causes a wrap around? 有人可以解释一下区别是什么,为什么赋值k=-k会导致回绕吗? It was hard to find a explanation online, as i don't really know what to google. 很难在网上找到解释,因为我真的不知道该怎么做。

Well, to print a short , you need to use a length modifier. 好吧,要打印 short ,您需要使用长度修饰符。

Change the format string to %hd . 将格式字符串更改为%hd

Because SHRT_MIN = -32768 and SHRT_MAX = +32767 . 因为SHRT_MIN = -32768SHRT_MAX = +32767 Check out how 2's complement work. 看看2的补码如何工作。

Edit 编辑

Actually, according to the international standard of the C programming language (ie Committee Draft — May 6, 2005 , pp. 50-51) regarding signed integers: 实际上,根据C语言的国际标准(即委员会草稿— 2005年5月6日 ,第50-51页),有关带符号整数:

For signed integer types, the bits of the object representation shall be divided into three groups: value bits, padding bits, and the sign bit. 对于有符号整数类型,对象表示的位应分为三组:值位,填充位和符号位。

There need not be any padding bits; 不需要任何填充位; there shall be exactly one sign bit. 必须有一个符号位。

Each bit that is a value bit shall have the same value as the same bit in the object representation of the corresponding unsigned type (if there are M value bits in the signed type and N in the unsigned type, then M <= N). 作为值位的每一位应具有与对应的无符号类型的对象表示形式中的相同位相同的值(如果有符号类型中有M个值比特,无符号类型中有N个值比特,则M <= N)。

If the sign bit is zero, it shall not affect the resulting value. 如果符号位为零,则它将不影响结果值。

If the sign bit is one, the value shall be modified in one of the following ways: 如果符号位为1,则应通过以下方式之一修改值:

  • the corresponding value with sign bit 0 is negated (sign and magnitude); 取反符号位0的对应值(符号和大小);

  • the sign bit has the value −(2 N ) (two's complement); 符号位的值为−(2 N )(二进制补码);

  • the sign bit has the value −(2 N − 1) (ones' complement). 符号位的值为-(2 N -1)(补码)。

Which of these applies is implementation-defined , as is whether the value with sign bit 1 and all value bits zero (for the first two), or with sign bit and all value bits 1 (for ones' complement), is a trap representation or a normal value. 其中哪一个是实现定义的 ,标志位为1且所有值位为零的值(对于前两个)还是标志位且所有值位为1(位的补码)是陷阱表示?或正常值。 In the case of sign and magnitude and ones' complement, if this representation is a normal value it is called a negative zero. 在符号,大小和“ 1”的补码情况下,如果此表示形式是正常值,则称为负零。

So, in other words, in your case it's seems that 2's complement is being used, but this should not be assumed to be in all platforms and compilers. 因此,换句话说,在您的情况下,似乎正在使用2的补码,但是不应假定在所有平台和编译器中都使用。

-32768 is 8000 in hexadecimal notation. -32768是十六进制8000 32768 can not be represented as a signed 16 bit number, therefore the compiler promotes -k to an int with the hex value 00008000 . 32768不能表示为带符号的16位数字,因此编译器将-k提升为十六进制值00008000int When this number is assigned to the 16-bit variable, the high bits are truncated, resulting in 8000 or -32768 again. 当此数字分配给16位变量时,高位将被截断,从而再次导致8000-32768

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

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