Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a mistake in the definition of the sign bit for integer types in the C standard?

I think there's an error describing the sign bit for integer types in section 6.2.6.2 of the ISO/IEC 9899:TC3 C 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 −(2^N) (two’s complement);
  • the sign bit has the value −(2^N − 1) (ones’ complement)

In the previous section, N was defined to be the number of value bits in the signed type, but here it's the number of value bits in the unsigned type.

Taking the case of signed char with 8 bits per byte and two's complement, this is saying that the sign bit has value -(2^8) =-256 rather than -(2^7) = -128.

I think the standard should either switch M and N in the initial paragraph or change the definition of the sign bit to use M:

  • the sign bit has the value −(2^M) (two’s complement);
  • the sign bit has the value −(2^M − 1) (ones’ complement)

Did I miss something, or is this an error?

like image 288
Paul Hankin Avatar asked Mar 24 '15 02:03

Paul Hankin


1 Answers

In the C11 draft standard(Jonathan Leffler confirms the final standard also contains this wording) it does indeed switch from using N to using M:

  • the sign bit has the value −(2M) (two’s complement);
  • the sign bit has the value −(2M − 1) (ones’ complement).

I can not find a defect report but this hinges on whether:

If there are N value bits

from paragraph 1 also applies to paragraph 2 which is not an unreasonable interpretation it is just highly ambiguous given:

(if there are M value bits in the signed type and N in the unsigned type, then M <= N)

like image 81
Shafik Yaghmour Avatar answered Oct 21 '22 20:10

Shafik Yaghmour