XOR checksum in arduino

Hi,

I have an array of 100 bytes. Does anybody know a way to create a XOR checksum in arduino?

Thanks

Initialise a variable to zero.
As each character comes in, XOR it with the variable, and store the result back to the variable.

AWOL:
Initialise a variable to zero.

byte checksm;
...
checksm = 0;

AWOL:
As each character comes in, XOR it with the variable, and store the result back to the variable.

checksm ^= value;

(Operator missing from the Arduino documentation.)

Paul__B:

byte checksm;

...
checksm = 0;





checksm ^= value;



(Operator missing from the Arduino documentation.)

Operator perfectly evident in Arduino documentation

AWOL:
Operator perfectly evident in Arduino documentation

Or in fact, not!

You have cited "bitwise XOR". The operator I showed was "compound bitwise xor" which is standard "C", is implemented in the language, but is not documented as such in the Arduino reference. If you can actually find it in there, you are a better man than I. :grinning:

There is very little gained in using an xor versus a simple sum if you have only one lateral check.

aarg:
There is very little gained in using an xor versus a simple sum if you have only one lateral check.

"lateral" ?

For me the advantage of XOR is that the value never grows beyond the size of a single byte.

...R

Robin2:
"lateral" ?

For me the advantage of XOR is that the value never grows beyond the size of a single byte.

...R

"Lateral" = bytewise vs. bitwise. For example, serial parity is bitwise. Choose your favourite description, by lateral I mean across the bytes. But a byte sized additive checksum need not grow, just allow it to overflow. It will still function correctly.

aarg:
But a byte sized additive checksum need not grow, just allow it to overflow. It will still function correctly.

OK, I was not thinking of it like that.

I have no idea whether that type of SUM or XOR is more likely to miss a single bit error in data transmission. I have no doubt there is a lot of literature on the subject but I have not studied it.

...R

I don't know how I felt such certainty about it. I have to admit I'm having second thoughts about it, now that you mention it. Maybe I should think it over. It does seem certain that the XOR would work.

The question would be, is there a bit in the payload that you could change, that would add or subtract a quantity consisting of multiples of the modulus of the parity byte. I think not.

Such multiples would be part of the higher order bytes of the checksum, if we were using them.

So for example, for the parity byte to be unchanged by a bit error in the payload, it would have to differ in the error byte by a 256 or a multiple of 256. For example, if one of the payload bytes had a bit error causing a 0 to become a 1 in the high bit, the difference would be 128. That would appear in the checksum byte, because it would not be part of the overflow. Same for subtraction, I think.

Any single error is by definition in a single byte. In that byte, the absolute difference from the checksum can only be 1,2,4,8,16,32,64, or 128. None of those differences cause an overflow with a remainder of 0.

Two of those errors would not be detected, because they add to 256, which is equal to the modulus of the parity bit. But we are not considering multiple errors.

So I can at least say that it could be a working deal. I'm not very good at proofs, and there are a lot of people nattering and chattering where I am right now. It's hard to concentrate. Do you see my reasoning? Any flaw?

I think this is a subject that needs "proper" research. This from Wikipedia is a start.

Personally I don't plan to do any further research.

...R

Hi,

Thank you for all of your replies.

I' have to base64 encoded the data and then calculate a crc (XOR) of that string.

If the base64 encoded string was:

char input = "Zm9vYmFy";

How would you calculate an XOR of this string?

Thanks

transcendglue:
char input = "Zm9vYmFy";

How would you calculate an XOR of this string?

Iterate through the array and XOR each element.

...R

Robin2:
I think this is a subject that needs "proper" research. This from Wikipedia is a start.

Personally I don't plan to do any further research.

...R

The method that I proposed is there, under "modular sum". So I have been granted the necessary golden seal of academia. :slight_smile: