title: Is the non-negative range of a signed C++ integer at least as big as the negative range?
body: <p>Does the C++ standard mandate that the <strong>non-negative</strong> range of a standard signed integer type is at least as big as the negative range?</p>
  
  <p>EDIT: Please note that I am referring to the <strong>non-negative</strong> range here, not the <strong>positive</strong> range which is obviously one smaller than the <strong>non-negative</strong> range.</p>
  
  <p>EDIT: If we assume C++11, the answer is "Yes". See my clarifications below. From the point of view of C++03, the answer is probably "No".</p>
  
  <p>The same question can be posed as follows: Does the standard guarantee that the result of <code>a - b</code> is representable in a standard signed integer type <code>T</code> assuming that both <code>a</code> and <code>b</code> are <strong>negative</strong> values of type <code>T</code>, and that <code>a ≥ b</code>?</p>
  
  <p>I know that the standard allows for two's complement, ones' complement, and sign magnitude representation of negative values (see C++11 section 3.9.1 [basic.fundamental] paragraph 7), but I am not sure whether it demands the use of one of those three representations. Probably not.</p>
  
  <p>If we assume one of these three representations, and we assume that there is no "spurious" restrictions on either of the two ranges (negative, and non-negative), then it is indeed true that the non-negative range is at least as big as the negative one. In fact, with two's complement the size of the two ranges will be equal, and with the two other representations, the size of the non-negative range will be one greater than the size of the negative one.</p>
  
  <p>However, even if we assume one of the mentioned representations, it is really not enough to guarantee anything about the size of either range.</p>
  
  <p>What I am seeking here, is a section (or set of sections) that unambiguously provides the desired guarantee.</p>
  
  <p>Any help will be appreciated.</p>
  
  <hr>
  
  <p>Note that something like the following would suffice: Every bit within the "storage slot" of the integer has one, and only one of the following functions:</p>
  
  <ul>
  <li>Unused</li>
  <li>Sign bit (exclusively, or mixed sign/value bit)</li>
  <li>Value bit (participating in the value)</li>
  </ul>
  
  <p>I have a vague memory that C99 says something along those lines. Anyone that knows anything about that?</p>
  
  <hr>
  
  <p>Alright, C99 (with TC3) does provide the necessary guarantees in section 6.2.6.2 "Integer types" paragraph 2:</p>
  
  <blockquote>
    <p>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:</p>
    
    <ul>
    <li>the corresponding value with sign bit 0 is negated (sign and magnitude);</li>
    <li>the sign bit has the value −(2<sup>N</sup> ) (two’s complement);</li>
    <li>the sign bit has the value −(2<sup>N</sup> − 1) (ones’ complement ).</li>
    </ul>
    
    <p>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.</p>
  </blockquote>
  
  <p>Can someone confirm that this part of C99 is also a binding part of C++11?</p>
  
  <hr>
  
  <p>I have taken another careful look at both the C99 and the C++11 standards, and it is clear that the guarantees in C99 section 6.2.6.2 paragraph 2 are binding in C++11 too.</p>
  
  <p>C89/C90 does not provide the same guarantees, so we do need C99, which means that we do need C++11.</p>
  
  <p>In summary, C++11 (and C99) provides the following guarantees:</p>
  
  <ol>
  <li><p>Negative values in fundamental signed integers types (standard + extended) <strong>must</strong> be represented using one of the following three representations: Two's complement, ones' complement, or sign magnitude.</p></li>
  <li><p>The size of the <strong>non-negative</strong> range is <strong>one</strong> greater than, or equal to the size of the negative range for all fundamental signed integers types (standard + extended).</p></li>
  </ol>
  
  <p>The second guarantee can be restated as follows:</p>
  
  <pre><code>-1 ≤ min&lt;T&gt; + max&lt;T&gt; ≤ 0
  </code></pre>
  
  <p>for any fundamental signed integers type <code>T</code> (standard + extended) where <code>min&lt;T&gt;</code> and <code>max&lt;T&gt;</code> are shorthands for <code>std::numeric_limits&lt;T&gt;::min()</code> and <code>std::numeric_limits&lt;T&gt;::max()</code> respectively.</p>
  
  <p>Also, if we assume that <code>a</code> and <code>b</code> are values of the same, or of different fundamental signed integer types (standard or extended), then it follows that <code>a - b</code> is well defined and representable in <code>decltype(a - b)</code> as long as <code>a</code> and <code>b</code> are either both negative or both non-negative.</p>