Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Signed integer variables do not have wrap-around behavior in C language. Signed integer overflow during arithmetic computations produces <em>undefined behavior</em>. Note BTW that GCC compiler you mentioned is known for implementing <em>strict overflow semantics</em> in optimizations, meaning that it takes advantage of the freedom provided by such undefined behavior situations: GCC compiler assumes that signed integer values never wrap around. That means that GCC actually happens to be one of the compilers in which you <em>cannot</em> rely on wrap-around behavior of signed integer types.</p> <p>For example, GCC compiler can assume that for variable <code>int i</code> the following condition</p> <pre><code>if (i &gt; 0 &amp;&amp; i + 1 &gt; 0) </code></pre> <p>is equivalent to a mere</p> <pre><code>if (i &gt; 0) </code></pre> <p>This is exactly what <em>strict overflow semantics</em> means.</p> <p>Unsigned integer types implement modulo arithmetic. The modulo is equal <code>2^N</code> where <code>N</code> is the number of bits in the value representation of the type. For this reason unsigned integer types do indeed appear to wrap around on overflow.</p> <p>However, C language never performs arithmetic computations in domains smaller than that of <code>int</code>/<code>unsigned int</code>. Type <code>unsigned short int</code> that you mention in your question will typically be promoted to type <code>int</code> in expressions before any computations begin (assuming that the range of <code>unsigned short</code> fits into the range of <code>int</code>). Which means that 1) the computations with <code>unsigned short int</code> will be preformed in the domain of <code>int</code>, with overflow happening when <code>int</code> overflows, 2) overflow during such computations will lead to undefined behavior, not to wrap-around behavior.</p> <p>For example, this code produces a wrap around</p> <pre><code>unsigned i = USHRT_MAX; i *= INT_MAX; /* &lt;- unsigned arithmetic, overflows, wraps around */ </code></pre> <p>while this code</p> <pre><code>unsigned short i = USHRT_MAX; i *= INT_MAX; /* &lt;- signed arithmetic, overflows, produces undefined behavior */ </code></pre> <p>leads to undefined behavior.</p> <p>If no <code>int</code> overflow happens and the result is converted back to an <code>unsigned short int</code> type, it is again reduced by modulo <code>2^N</code>, which will appear as if the value has wrapped around.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload