【发布时间】:2018-09-07 19:48:55
【问题描述】:
我试图通过以下方式了解 CRC 表查找优化:http://www.sunshine2k.de/articles/coding/crc/understanding_crc.html#ch5,特别是:
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;
}
我了解基本的逐位方法,也了解查找方法,但我很难理解这一行:
crc = (ushort)((crc << 8) ^ (ushort)(crctable16[pos]));
为什么需要对现有的 crc 进行 rshift?为什么这个技巧有效?它的依据是什么?
谢谢。
PS:是的,我看了这个帖子:How is a CRC32 checksum calculated?(这不是骗子,因为它描述了基本方法)
【问题讨论】:
-
您参考的答案中链接的无痛指南很好地解释了这一点。