Search code examples
algorithmchecksumcrcbasicinteger-arithmetic

Checksum without logic/bitwise operations


I need to calculate a 1 byte checksum over 15 bytes. My first idea was to use something like crc8, but the problem is that I have to use a really limited BASIC interpreter which only supports basic arithmetic operations (+-*/) but no bitwise "xor", "and" or "or" and no "shift" operations.

So my question is: is it possible to calculate a somewhat reasonable checksum with these limitations? I think calculating the sum would be a poor solution, but I can't think of something else at the moment.


Solution

  • LET a = 1
    LET b = 0
    FOR n = 1 to 15
       LET a = a + c(n)
       LET b = b + a
    NEXT n
    LET b = mod(b, 251)
    

    Then use b as the check value. The advantage of this over a simple sum is that it is not commutative. The order of the bytes matter. Also a string of zeroes does not give you zero, and the check value depends on how many zeros.