Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With these definitions and assuming <code>unsigned short</code> == 16 bit and <code>int</code> == 16 bit and <code>unsigned long</code> == 32 bit (note that the size of <code>int</code> is important):</p> <pre><code>unsigned short a, b; unsigned long c; </code></pre> <p>These two statements will do different things:</p> <pre><code>c = (unsigned long)(a + b); c = (unsigned long)a + b; </code></pre> <p>The first one -- the one you showed -- will first add the numbers as unsigned shorts, discarding any overflow -- and <em>then</em> expand to an unsigned long. This may be why you're asking. The second will cast <code>a</code> to an unsigned long, then add the two numbers, expanding <code>b</code> to an unsigned long as well before doing the addition.</p> <p>You could also do this:</p> <pre><code>c = (unsigned long)a + (unsigned long)b; </code></pre> <p>but it's unnecessary as the presence of one unsigned long in the calculation will cause the other to be automatically promoted from short to long size.</p> <p>Thus:</p> <pre><code>a = 0x8001; b = 0x8001; c = (unsigned long)(a + b); /* c is assigned 0x00000002 */ c = (unsigned long)a + b; /* c is assigned 0x00010002 */ </code></pre> <p>All of this depends on your specific compiler and on the size of its variables. It depends specifically on the size of an <code>int</code> on your compiler, as all values are automatically promoted to <code>int</code> or <code>unsigned int</code> during calculations. You mentioned hardware with 16 bit accumulators, and C compilers will typically (but not always) use the native register size as the size of <code>int</code>, which is why I assumed an <code>int</code> as 16 bits.</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. 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