title: Converting CRC16 calculation from C to C#
body: <p>I have to translate a CRC16 calculation from C to C# but get the message that I cannot implicitly convert type 'int' to 'bool' on the <code>(crc &amp; 0x8000)</code> and on <code>return (crc &amp; 0xFFFF)</code></p>
  
  <p>The code so far:</p>
  
  <pre><code>public unsafe short Crc16(string str)
  {
      short crc = 0;
  
      for(int i = 0; i &lt; str.Length; i++)
      {
          crc = (crc &lt;&lt; 1) ^ str[i] ^ ((crc &amp; 0x8000) ? 0x1021 : 0);
      }
      return (crc &amp; 0xFFFF);
  }
  </code></pre>
  
  <p>EDIT: Changed char parameter to string</p>
  
  <p>Original C code</p>
  
  <pre><code>short Crc16( char *str )
  {
      short crc=0;
      unsigned int i;
      for (i=0; i&lt;strlen(str); i++)
          crc = (crc &lt;&lt; 1) ^ *(str+i) ^ ((crc &amp; 0x8000)? 0x1021:0 );
      return (crc &amp; 0xffff);
  }
  </code></pre>