Short background to my question: I'm working on a small hobby project with a microcontroller communicating with a PC over UART. Right now I'm using COBS byte stuffing with 0x00 packet delimiter bytes and a simple 1-byte checksum that is either a two's complement running sum or a one's complement sum. My one's complement checksum implementation is very similar to Internet checksum (page 7. The interesting part is seen below)
// Fold 32-bit sum to 16 bits //
while (sum>>16)
sum = (sum & 0xffff) + (sum >> 16);
checksum = ~sum;
A few days ago I found out about the Fletcher checksum:
Fletcher article
Fletcher implementation
Fletcher Wikipedia
Short code snippet from the Fletcher Wiki page:
for( index = 0; index < count; ++index )
{
sum1 = (sum1 + data[index]) % 255;
sum2 = (sum2 + sum1) % 255;
}
return (sum2 << 8) | sum1;
My question: In both articles above they say that Fletcher uses "one's complement [mod(255)] checksums" as though a mod-255 and one's complement sum are the same. Is that really true?
- It makes sense to me that the carry bits that makes one's complement checksums superior, could work in pretty much the same way in a mod-255 sum compared to the one's complement adder above. But with a mod-255 sum you can never get the value 0xFF (-0), only 0x00 (+0)?
- I guess that the mod-operator is slower (although it's linear so you can wait with the mod- calculation until the end of the sum).
- A nice feature (when using COBS) is that mod-255 will never produce 0x00 check bytes since the mod-255 sum can never be 0xFF (although that's easily fixed even in the one's complement adder above).
Thank you so much for your time!
Kind regards / Henrik