title: Cast from "int" to "unsigned short" after applying bitwise operator "~"
body: <p>Static analysis tool I use raises a warning for this code :</p>
  
  <pre><code>uint16 var1 = 1U;
  uint16 var2 = ~var1;
  </code></pre>
  
  <p>I check MISRA C 2004 rules and I find 10.5 rule :</p>
  
  <blockquote>
    <p>If the bitwise operators ~ and &lt;&lt; are applied to an operand od underlying type <em>unsigned char</em> or <em>unsigned short</em>, the result shall be immediately cast to the underlying type of the operand.</p>
  </blockquote>
  
  <p>Ok, it's not a problem, implicit cast is applied (I think "cast" means implicit or explicit cast). But 10.1 rule says :</p>
  
  <blockquote>
    <p>The value of an expression of integer type shall not be implicitly converted to a different underlying type the expression is complex.</p>
  </blockquote>
  
  <p>An previous example of complex operation are : ~u16a</p>
  
  <p>I change my code to :</p>
  
  <pre><code>uint16 var1 = 1U;
  uint16 var2 = (uint16) ~var1;
  </code></pre>
  
  <p>And I obtain another warning : I think conversion of int negative value to unsigned int value not safe. I check C99 standard (<a href="http://www.open-std.org/jtc1/sc22/wg14/www/docs/C99RationaleV5.10.pdf">ISO C99</a>) § 6.3.1.3 but I don't understand if conversion of <em>int</em> to <em>unsigned short</em> are clearly specified.</p>
  
  <p>In <a href="http://embeddedgurus.com/stack-overflow/2009/08/a-tutorial-on-signed-and-unsigned-integers/">EmbeddedGurus article</a> I read :</p>
  
  <pre><code>c = (unsigned int) a; /* Since a is positive, this cast is safe */
  </code></pre>
  
  <p>My questions : </p>
  
  <ol>
  <li>Have explicit conversion from <em>signed int</em> to <em>unsigned short</em> unspecified behavior ? </li>
  <li>If yes, how to use complement operator with unsigned short in safe way ?</li>
  </ol>