title: How to configure calculation of CRC table
body: <p>There are a lot of CRC calculation examples out there. Simple implementations with bit shifting and more efficient with a pre-calculated table. But there are also a lot of Parameters of a CRC beside the polynomial that affect the calculation. You can evaluate these parameters here: <a href="http://zorc.breitbandkatze.de/crc.html" rel="nofollow">http://zorc.breitbandkatze.de/crc.html</a></p>
  
  <p>These parameters are</p>
  
  <ul>
  <li>initial value of CRC</li>
  <li>reflection of input data</li>
  <li>reflection of output data</li>
  <li>final XOR value for CRC</li>
  </ul>
  
  <p>For some "standard" CRC algorithm these parameters are well defined, like CRC-16 (CCITT). But there are some implementations that use different parameters. </p>
  
  <p>My implementation has to be compatible with a CRC16 with a CCITT polynomial (x<sup>16</sup> + x<sup>12</sup> + x<sup>5</sup> + 1). But the data bytes and the final CRC must be reflected. I have implemented these reflection in the calculation method. But that is time consuming. For best performance it must be removed from the calculation.</p>
  
  <p>How can the reflection parameters of the CRC be calculated in the initialization method?</p>
  
  <p><em>Edit:</em> How should I do to control each of the parameters individually? I would like to understand how the <code>Init</code> function actually works and how all the parameters are implemented.</p>
  
  <pre><code>typedef unsigned char uint8_t;
  typedef unsigned short crc;
  
  crc  crcTable[256];
  #define WIDTH  (8 * sizeof(crc))
  #define TOPBIT (1 &lt;&lt; (WIDTH - 1))
  #define POLYNOMIAL 0x1021
  
  template&lt;typename t&gt;
  t reflect(t v)
  {
      t r = 0;
  
      for (int i = 0; i &lt; (8 * sizeof v); ++i)
      {
          r &lt;&lt;= 1;
          r |= v&amp;1;
          v &gt;&gt;= 1;
      }
  
      return r;
  }
  
  void Init()
  {
      crc  remainder;
  
      for (int dividend = 0; dividend &lt; 256; ++dividend)
      {
          remainder = dividend &lt;&lt; (WIDTH - 8);
  
          for (uint8_t bit = 8; bit &gt; 0; --bit)
          {
              if (remainder &amp; TOPBIT)
                  remainder = (remainder &lt;&lt; 1) ^ POLYNOMIAL;
              else
                  remainder = (remainder &lt;&lt; 1);
          }
  
          crcTable[dividend] = remainder;
      }
  }
  
  crc Calculate(const uint8_t *message, int nBytes, crc wOldCRC)
  {
      uint8_t data;
      crc remainder = wOldCRC;
  
      for (int byte = 0; byte &lt; nBytes; ++byte)
      {
          data = reflect(message[byte]) ^ (remainder &gt;&gt; (WIDTH - 8));
          remainder = crcTable[data] ^ (remainder &lt;&lt; 8);
      }
  
      return reflect(remainder);
  }
  
  int main()
  {
      crc expected = 0x6f91;
      uint8_t pattern[] = "123456789";
  
      Init();
      crc result = Calculate(pattern, 9, 0xFFFF);
  
      if (result != expected)
      {
          // this output is not relevant to the question, despite C++ tag
          printf("CRC 0x%04x wrong, expected 0x%04x\n", result, expected);
      }
  }
  </code></pre>