Note that there are some explanatory texts on larger screens.

plurals
  1. POUnexpected bit shift result on 8051 (8-bit) microcontroller
    primarykey
    data
    text
    <p>Consider the following code:</p> <pre><code>#include &lt;hal_types.h&gt; int main() { uint16 crc16; // hal_types.h: typedef unsigned short uint16; crc16 = 0x43; // debugger: crc16 == 0x0043, as expected crc16 = crc16 &lt;&lt; 8; // crc16 == 0x0000 ???? return 0; } </code></pre> <p>This code is running on a TI CC1111 SoC (with an 8051 core), and compiled/debugged using IAR EW8051 8.10.3, configured to use the C99 dialect, with no optimization. The values in the comments were observed with the IAR debugger (same results using either simulator or actual device). </p> <p>I would expect that after <code>crc16 = crc16 &lt;&lt; 8;</code>, <code>crc16</code> would have the value <code>0x4300</code>, not <code>0x0000</code>. </p> <p>According to the C99 standard (well, the May 2005-05-06 draft), section 6.5.7.3-4.</p> <blockquote> <p>The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.</p> <p>The result of E1 &lt;&lt; E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. If E1 has an unsigned type, the value of the result is E1 × 2^E2, reduced modulo one more than the maximum value representable in the result type. If E1 has a signed type and nonnegative value, and E1 × 2^E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.</p> </blockquote> <p>My take on this is that the result type should be an unsigned 16-bit integer, with the value <code>((0x0043)*(2^8)) % 0x10000 == 0x4300</code>. </p> <p>Am I missing something? Thanks.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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