I'm trying to understand CRC table lookup optimization by going through: http://www.sunshine2k.de/articles/coding/crc/understanding_crc.html#ch5, particularly:
public static ushort Compute_CRC16(byte[] bytes)
{
ushort crc = 0;
foreach (byte b in bytes)
{
/* XOR-in next input byte into MSB of crc, that's our new intermediate divident */
byte pos = (byte)( (crc >> 8) ^ b); /* equal: ((crc ^ (b << 8)) >> 8) */
/* Shift out the MSB used for division per lookuptable and XOR with the remainder */
crc = (ushort)((crc << 8) ^ (ushort)(crctable16[pos]));
}
return crc;
}
I do understand the basic bit-by-bit approach and I do understand the lookup method, but I'm having a hard time to catch this line:
crc = (ushort)((crc << 8) ^ (ushort)(crctable16[pos]));
why is the existing crc needs to be rshifted? Why does this trick work? What is it based on?
Thanks.
PS: yes, I read this thread: How is a CRC32 checksum calculated? (it's not a dupe, because it describes the basic method)