??? 08/12/07 17:58 Read: times |
#143127 - Your observations plus a comparison in C Responding to: ???'s previous message |
Russ Cooper said:
Have you run into articles like this one that raise some doubt about how a correct implementation of CCITT CRC-16 is really supposed to work? It points out that many websites (including the online CRC calculator that you have mentioned) give 29B1h as the CRC for the test string "123456789". However, the author of the site believes for one reason or another that the correct result is actually E5CCh.
I don't suppose this would matter too much if your use of the algorithm was limited only to your own programs. Of course it would matter a lot if you were exchanging data with somebody else's program that calculated the CRCs differently. If I recall correctly, that author's conjecture about the correct result is know to be wrong. Your issue about needing to know all the parameters for correct implementation (initial value, bit processing order, complementing final value, etc.) is well founded. Here's a C implementation of the type of algorithm that Jan posted, but that processes the bits in the LSBit-first order as they would be on-the-wire for UART exchange and as defined by the HDLC standard(s). It has been tested against HDLC protocol test equipment. I have adapted it to be most similar to Jan's code for the purpose of seeing how poorly the C version performs in comparison (28 cycles). unsigned char CRCLo, CRCHi; void CRCNib(unsigned char b) { CRCLo ^= b; b = CRCHi; CRCHi = CRCLo ^ (CRCLo << 4); CRCLo = b; CRCLo ^= (b = CRCHi >> 4); b = CRCHi ^ (b >> 1); CRCLo ^= CRCHi << 3; CRCHi = b; } |