I'm working on reverse-engineering an old file transfer protocol, with
Teensy 2.0.
I found a document which has what's claimed to be the CRC algorithm.
It does in fact work. I can monitor & record transactions, and the
algorithm gives the CRCs observed in the packets exchanged.
There's a line of code in the algorithm that worries me, though, be-
cause I don't fully understand it -
crc^=Byte;
for(i=0;i<8;i++) crc = (crc >> 1) ^ ((crc & 1)!=0 ? polynomial : 0); // ouch
Which lives inside a loop which feed it bytes from the packet being received.
I've tried re-stating it, and found this also gives a correct answer -
for ( i = 0; i < 8; i++ ) // ouch re-stated...
{
bit0 = crc & 1;
crc = crc >> 1;
if ( bit0 ) { crc = crc ^ polynomial; }
}
However, it is'nt glaringly obvious to me that the two statements are equivalent.
Can anyone see what I'm missing, or debunk it if it's wrong?
thanks, Jack
Teensy 2.0.
I found a document which has what's claimed to be the CRC algorithm.
It does in fact work. I can monitor & record transactions, and the
algorithm gives the CRCs observed in the packets exchanged.
There's a line of code in the algorithm that worries me, though, be-
cause I don't fully understand it -
crc^=Byte;
for(i=0;i<8;i++) crc = (crc >> 1) ^ ((crc & 1)!=0 ? polynomial : 0); // ouch
Which lives inside a loop which feed it bytes from the packet being received.
I've tried re-stating it, and found this also gives a correct answer -
for ( i = 0; i < 8; i++ ) // ouch re-stated...
{
bit0 = crc & 1;
crc = crc >> 1;
if ( bit0 ) { crc = crc ^ polynomial; }
}
However, it is'nt glaringly obvious to me that the two statements are equivalent.
Can anyone see what I'm missing, or debunk it if it's wrong?
thanks, Jack