title: 16-bit CRC-CCITT in Matlab
body: <p>Given the <code>calcCRC()</code> C function shown below, what is the equivalent Matlab function?</p>
  
  <p><strong>16-bit CRC-CCITT in C:</strong></p>
  
  <pre><code>/*
   * FUNCTION: calcCRC calculates a 2-byte CRC on serial data using
   * CRC-CCITT 16-bit standard maintained by the ITU
   * ARGUMENTS: queue_ptr is pointer to queue holding are a to be CRCed
   * queue_size is offset into buffer where to stop CRC calculation
   * RETURNS: 2-byte CRC
   */
  unsigned short calcCRC(QUEUE_TYPE *queue_ptr, unsigned int queue_size) {
      unsigned int i=0, j=0;
      unsigned short crc=0x1D0F; //non-augmented initial value equivalent to augmented initial value 0xFFFF
  
      for (i=0; i&lt;queue_size; i+=1) {
          crc ^= peekByte(queue_ptr, i) &lt;&lt; 8;
  
          for(j=0;j&lt;8;j+=1) {
              if(crc &amp; 0x8000) crc = (crc &lt;&lt; 1) ^ 0x1021;
              else crc = crc &lt;&lt; 1;
          }
      }
  
      return crc;
  }
  </code></pre>
  
  <p>Below is the Matlab code I have come up with that seems to be equivalent but does not output the same results:</p>
  
  <p><strong>(Incorrect) 16-bit CRC-CCITT in Matlab:</strong></p>
  
  <pre><code>function crc_val = crc_ccitt_matlab (message)
      crc = uint16(hex2dec('1D0F'));
  
      for i = 1:length(message)
          crc = bitxor(crc,bitshift(message(i),8));
  
          for j = 1:8
              if (bitand(crc, hex2dec('8000')) &gt; 0)
                  crc = bitxor(bitshift(crc, 1), hex2dec('1021'));
              else
                  crc = bitshift(crc, 1);
              end
          end
      end
  
      crc_val = crc;
  end
  </code></pre>
  
  <p>Here is a sample byte array, represented as an integer array:</p>
  
  <pre><code>78 48 32 0 251 0 215 166 201 0 1 255 252 0 1 2 166 255 118 255 19 0 0 0 0 0 0 0 0 0 0 0 0 3 0
  </code></pre>
  
  <p>The expected output is two bytes <code>base10(44 219)</code> which is <code>base2(00101100 11011011)</code> or <code>base10(11483)</code>.</p>
  
  <p>My Matlab function gives <code>base10(85)</code> which is <code>base2(00000000 01010101)</code>.</p>
  
  <p>Any ideas on what is causing the output to not be the expected?</p>