int的最小值不是-32768?
而且c primer plus说的也前后不一致
而且c primer plus说的也前后不一致
解决方案
20
5.2.4.2.1 Sizes of integer types <limits.h>
1 The values given below shall be replaced by constant expressions suitable for use in #if
preprocessing directives. Moreover, except for CHAR_BIT and MB_LEN_MAX, the
following shall be replaced by expressions that have the same type as would an
expression that is an object of the corresponding type converted according to the integer
promotions. Their implementation-defined values shall be equal or greater in magnitude
(absolute value) to those shown, with the same sign.
标准只是给了一个范围限制而已,并没有规定一定是这个值。
至于为什么是 -32767 而不是 -32768 ,原因是标准对有符号数允许三种不同的表示方式,原码、反码、补码(最常用的)。
16bit 原码与反码都无法表示 -32768。
6.2.6.2 Integer types
2 ………………………. If the sign bit is one, the value shall be
modified in one of the following ways:
— the corresponding value with sign bit 0 is negated (sign and magnitude); // 原码
— the sign bit has the value −(2^N) (two’s complement); // 补码
— the sign bit has the value −(2^N − 1) (one’s complement). // 反码
另外,标准允许在在有符号数中,用某一个值表示异常:(原码和补码的 10000000 ,反码的 1111111111)
…. 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 one’s
complement), is a trap representation or a normal value.
假如使用异常值的话,三种编码都无法表示 -32768 。
1 The values given below shall be replaced by constant expressions suitable for use in #if
preprocessing directives. Moreover, except for CHAR_BIT and MB_LEN_MAX, the
following shall be replaced by expressions that have the same type as would an
expression that is an object of the corresponding type converted according to the integer
promotions. Their implementation-defined values shall be equal or greater in magnitude
(absolute value) to those shown, with the same sign.
标准只是给了一个范围限制而已,并没有规定一定是这个值。
至于为什么是 -32767 而不是 -32768 ,原因是标准对有符号数允许三种不同的表示方式,原码、反码、补码(最常用的)。
16bit 原码与反码都无法表示 -32768。
6.2.6.2 Integer types
2 ………………………. If the sign bit is one, the value shall be
modified in one of the following ways:
— the corresponding value with sign bit 0 is negated (sign and magnitude); // 原码
— the sign bit has the value −(2^N) (two’s complement); // 补码
— the sign bit has the value −(2^N − 1) (one’s complement). // 反码
另外,标准允许在在有符号数中,用某一个值表示异常:(原码和补码的 10000000 ,反码的 1111111111)
…. 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 one’s
complement), is a trap representation or a normal value.
假如使用异常值的话,三种编码都无法表示 -32768 。