title: CRC 16 bit (Polynomial 0x1021) CCITT calculation with initial value 0x0000
body: <p>Well I am scratching my head and very close to break it with hammer. I am trying to calculate CRC16 bit CCITT and it not giving me what its supposed to. I have searched almost everything and dont know what i am missing. </p>
  
  <p>This is the full packet with last two bytes of CRC calculation </p>
  
  <blockquote>
    <p>54 A1 4A 03 E7 03 D4 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 64 CA</p>
  </blockquote>
  
  <p>for now i am adding sync byte which is 0x54 if I dont add sync byte the packet would be </p>
  
  <blockquote>
    <p>54 A1 4A 03 E7 03 D4 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 65 CF</p>
  </blockquote>
  
  <p>both CRC values (64 CA &amp;&amp; 65 CF) are wrong. I supposed to get 72 DC &amp;&amp; 99 3F respectively.</p>
  
  <p>this is the code I am using to calculate CRC16 bit</p>
  
  <pre><code>private static ushort CRCCalc(byte[] data)
      {
          ushort wCRC = 0;
          for (int i = 1; i &lt; data.Length; i++)
          {
              wCRC = (ushort)(wCRC ^ (data[i] &lt;&lt; 8));
  
              for (int j = 0; j &lt; 8; j++)
              {
                  if ((wCRC &amp; 0x8000) != 0)
                      wCRC = (ushort)((wCRC &lt;&lt; 1) ^ 0x1021);
                  else
                      wCRC &lt;&lt;= 1;
              }
          }
          return wCRC;
      }
  </code></pre>
  
  <p>Any Comments...?</p>
  
  <p>Thanks</p>