title: Calculating CRC16 checksum
body: <p>I'm reading (binary) data from file and generating the CRC16 checksum. The next step is to write this checksum at the end of this file (as last 2 bytes) and then calculate CRC16 again which should be 0. The problem is the CRC which i wrote to the file is different. For example i use <code>write(reinterpret_cast&lt;const char *&gt;(&amp;crc), 2)</code> to write<code>short int crc = 0xba10</code> at the end, but in fact im writing <code>ş</code> which is
  <code>00010000 11000101 10011111</code> not <code>10111010 00010000</code>.Is there any way to write it properly at the end of file?</p>
  
  <p>Here is my crc calculating code:</p>
  
  <pre><code>int crc16(char* data_p, int length){
  unsigned char x;
  unsigned short crc = 0x1D0F; 
  
  while (length--){
      x = crc &gt;&gt; 8 ^ *data_p++;
      x ^= x &gt;&gt; 4; // x = x ^ (x &gt;&gt; 4)
      crc = (crc &lt;&lt; 8) ^ ((unsigned short)(x &lt;&lt; 12)) ^ ((unsigned short)(x &lt;&lt;5)) ^ ((unsigned short)x);
  }
  return crc;
  }
  </code></pre>