Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>C 2011 (n1570) 6.3.1.8 (“Usual arithmetic conversions”) 1 states that the integer promotions are performed <em>before</em> considering whether the types are the same:</p> <blockquote> <p>Otherwise, the integer promotions are performed on both operands. Then the following rules are applied to the promoted operands:</p> <blockquote> <p>If both operands have the same type, then no further conversion is needed…</p> </blockquote> </blockquote> <p>Thus, in the C abstract machine, <code>unsigned char</code> values must be promoted to <code>int</code> before arithmetic is performed. (There is an exception for perverse machines where <code>unsigned char</code> and <code>int</code> have the same size. In this case, <code>unsigned char</code> values are promoted to <code>unsigned int</code> rather than <code>int</code>. This is esoteric and need not be considered in normal situations.)</p> <p>In the actual machine, operations must be performed in a way that gets the same results <em>as if</em> they were performed in the abstract machine. Because only the results matter, the actual intermediate operations do not need to exactly match the abstract machine.</p> <p>When the sum of two <code>unsigned char</code> values is assigned to an <code>unsigned char</code> object, the sum is converted to a <code>unsigned char</code>. This conversion essentially discards bits beyond the bits that fit in an <code>unsigned char</code>.</p> <p>This means that the C implementation gets the same result whether it does this:</p> <ul> <li>Convert values to <code>int</code>.</li> <li>Add values with <code>int</code> arithmetic.</li> <li>Convert result to <code>unsigned char</code>.</li> </ul> <p>or this:</p> <ul> <li>Add values with <code>unsigned char</code> arithmetic.</li> </ul> <p>Because the result is the same, the C implementation may use either method.</p> <p>For comparison, we can consider this statement instead: <code>int c = a + b;</code>. Also, suppose the compiler does not know the values of <code>a</code> and <code>b</code>. In this case, using <code>unsigned char</code> arithmetic to do the addition could yield a different result than converting the values to <code>int</code> and using <code>int</code> arithmetic. E.g., if <code>a</code> is 250 and <code>b</code> is 200, then their sum as <code>unsigned char</code> values is 194 (250 + 200 % 256), but their sum in <code>int</code> arithmetic is 450. Because there is a difference, the C implementation <strong>must</strong> use instructions that get the correct sum, 450.</p> <p>(If the compiler did know the values of <code>a</code> and <code>b</code> or could otherwise prove that the sum fit in an <code>unsigned char</code>, then the compiler could again use <code>unsigned char</code> arithmetic.)</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