-32768 not fitting into a 16 bit signed value

前端 未结 4 1013
轻奢々
轻奢々 2021-01-23 15:40

I am using PCLint v 9.00h

In my code I have the following (where S16 is a signed 16 bit):

S16 temperatureResult = -32768;

Which unless

4条回答
  •  我在风中等你
    2021-01-23 15:59

    First of all: it doesn't have to deal with -32768 by standard:

    5.2.4.2.1 Sizes of integer types

    [...]

    — minimum value for an object of type short int

    SHRT_MIN -32767 // -(215 - 1)

    — maximum value for an object of type short int

    SHRT_MAX +32767 // 215 - 1

    (I'm looking for the part which makes a enviroment defined note about supporting -32768 anyway)

    Got it:

    The reason why there is sometimes anyway one more number supported is justified by this paragraph:

    6.2.6.2 Integer types

    [...]

    2 — 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). 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:

    — the corresponding value with sign bit 0 is negated (sign and magnitude);

    — the sign bit has the value -(2N) (two’s complement);

    — the sign bit has the value -(2N - 1) (ones’ complement).

    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. In the case of sign and magnitude and ones’ complement, if this representation is a normal value it is called a negative zero.

    (All I'm quoting is written in ISO/IEC 9899:TC3)

提交回复
热议问题