Does the C++ standard requires signed integers to have exactly one sign bit?
c++, c++11, integer, language-lawyer, standards
Solution
This is what C++11 says about representation of signed integer types:
C++11 N3337 3.9.1 [basic.fundamental] P7:
The representations of integral types shall define values by use of a pure binary numeration system. 49 [ Example: this International Standard permits 2’s complement, 1’s complement and signed magnitude representations for integral types. — end example ]
where Footnote 49 reads:
- A positional representation for integers that uses the binary digits 0 and 1, in which the values represented by successive bits are additive, begin with 1, and are multiplied by successive integral power of 2, except perhaps for the bit with the highest position. (Adapted from the American National Dictionary for Information Processing Systems.)
Thus C++ allows the same three options as C, as well as anything else satisfying Footnote 49. Which is a superset of what C allows. By Footnote 49, however, only the highest bit is allowed to have special meaning.
Update 2021-10-11
C++20 has introduced changes regarding allowed representations of signed integers.
N4860 [basic.fundamental/p3] reads:
An unsigned integer type has the same object representation, value representation, and alignment requirements (6.7.6) as the corresponding signed integer type. For each value x of a signed integer type, the value of the corresponding unsigned integer type congruent to x modulo 2 N has the same value of corresponding bits in its value representation. 40 [Example: The value −1 of a signed integer type has the same representation as the largest value of the corresponding unsigned type. — end example]
And the footnote 40 reads:
- This is also known as two’s complement representation.
Therefore, C++ now mandates two's complement representation of signed integer types.
Problem
Consider the fundamental signed integer types of C++, namely: `signed char`, `short int`, `int`, `long int` and `long long int`, what does the current C++ standard require about their underlying bit representation? Does the constraints on their bit representation specify that they should include: - optional padding bits - mandatory value bits - a mandatory sign bit that is `0` for positive values, and `1` for negative value - if it exists, the sign bit should be the most significant bit Is this true? If not, then what are the constaints? I am searching for quotes from the standard that proves or disproves this. EDIT: I am asking this question, because, in C, the standard says: 6.2.6.2.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; signed char shall not have 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^M ) (two’s complement); - the sign bit has the value −(2^M − 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. So I am wondering whether something comparable exists in C++