在GCC里有个CRC16校验头文件<util/crc16.h>,里面定义了3个校验函数,其中一个是这样的:
Polynomial: x^16 + x^12 + x^5 + 1 (0x1021)<br>
Initial value: 0x0
This is the CRC used by the Xmodem-CRC protocol.
The following is the equivalent functionality written in C.
\code
uint16_t
crc_xmodem_update (uint16_t crc, uint8_t data)
{
int i;
crc = crc ^ ((uint16_t)data << 8);
for (i=0; i<8; i++)
{
if (crc & 0x8000)
crc = (crc << 1) ^ 0x1021;
else
crc <<= 1;
}
return crc;
}
\endcode */
其中的这条语句-crc = crc ^ ((uint16_t)data << 8);是什么作用呢?看了好长时间也没能想明白,期待高手帮忙解答! |