How to create a Checksum using logical XOR

Hi,

How do you create a checksum using the logical "XOR" of all 32-bit words in a binary file? I would like to implement this in C++ rather than use a tool.

Thank you
Hi,

At a guess, read the file in as a 32 bit integer array, the run an iterative loop xoring the next byte with a byte used to store the final checksum. Cant remeber what the operator is for xor but sure google will help you there.
The C++ XOR operator is ^

You could simply XOR every integer together starting with a 0 int and XORing it with every word in the file.
Hi thank you for the responses. I am not very familiar with low level programming. What do you mean by "every integer together starting with a 0 int"?

Is it something like:

int checksum = 0;
for each integer in the file
{
checksum = checksum ^ currentInteger;
}

for each word in the file
{
checksum = checksum ^ currentWord;
}

Last edited on
Yes. I was a bit confusing really. You said you wanted to checksum all the 32bit words.

I freely referred to them as both integers and words. I meant the same thing.

So BOTH your loops are the same by what I meant and you would therefore only need one of them.


1
2
3
4
5
int checksum = 0;
for each integer in the file
{
    checksum = checksum ^ currentInteger;
}


That would do nicely.
Topic archived. No new replies allowed.