How are negative numbers represented in 32-bit signed integer?

后端 未结 7 1889
时光取名叫无心
时光取名叫无心 2020-12-02 01:30

How are negative number represented in 32-bit signed integer? Is it two\'s or one\'s complement? or the last bit on the left is like a flag? For example: (-10)

7条回答
  •  孤街浪徒
    2020-12-02 02:19

    From the C99 standard:

    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.

提交回复
热议问题