What that does is to check the least significant bit of crc
and then negating it. The effect is that if the bit is zero the mask
will be zero (that is all zeroes) and if the bit is one the mask
will be -1
(that is all ones). This is used to conditionally xor with 0xEDB88320
.
The other solution instead uses if
to make that condition.
The second trick they're using in the second solution is to do the xor for the bit check in one operation for all eight bits. In the first example they use (int)(crc^byte) < 0
(which means a check for the XOR of the most significant bit or the sign bit), they then shift both crc
and byte
one bit to the left and do the same on next bit. In the second example they do the XOR eight bits at a time and then checks each bit of the result.
To see what happens, consider if we change the first example to:
for(j=0; j<=7; j++) {
crc = crc ^ mask_sign_bit(byte);
if( (int)crc < 0 )
crc = (crc << 1) ^ 0x04C11DB7;
else
crc = crc << 1;
byte = byte << 1;
}
where mask_sign_bit
masks out every bit except the sign bit, the sign of crc ^ byte
becomes the same as crc ^ mask_sign_bit(byte)
so the consequence of the if
statement becomes the same. Then when shifting crc
to the left one step the bit modified by crc = crc ^ mask_sign_bit(byte)
will be lost.