# Thread: Question: Bits and C++

1. ## Question: Bits and C++

Okay.. here's the question - what's the easist way of making an XOR checksum byte for a string.

I would think some think like this:

Code:
```int hold;
int checksum = string[0];
for(int i=1, i &gt; strlen(string)-1, i++)
{
hold = checksum;
checksum = string[i] ^ hold;
}```
Problem - ^ is a bitwise operator and a byte is 8 bits, and we need the result of xoring the bytes indidually not the bits indvidually.

I figured that Xoring the string and appending my checksum as an int, cast as a char - it should be safe as the int never will be bigger then the byte can hold.

(I have to convert my ints to chars to be able to use them futher on in my system - I'll post the entire code later for you guys to laugh at)

basic idea:

Pack string in HDLC compliant format, send to external unit,

string -&gt; stick - DLE, STX, Adresse of external unit, Control field (will be a bitfield), ID, PAYLOAD (our string), ETX and Checksum. -&gt; HEY HO... off to I/O we go..

2. ## Re: Question: Bits and C++

I'm confused as to what you mean by XORing a byte, if not bit by bit.

3. ## Re: Question: Bits and C++

Why not dump the individual chars into a vector? Or make a class for the correct output? Of course, I'm still fuzzy on what you are trying to do...

4. ## Re: Question: Bits and C++

No problem, I think I got a solution which is much cleaner.

Just need to debug the code a bit.

5. ## Re: Question: Bits and C++

If your complaint is about using a 16-bit integer to make checksums byte by byte, then don't use an integer. Use another char.

6. ## Re: Question: Bits and C++

If your complaint is about using a 16-bit integer to make checksums byte by byte, then don't use an integer. Use another char.
oh no... if had to choose I wouldn't even do it XOR style... I would use a proper CRC16/32 checksum, the assignment requires me to use one char XOR checksum - 8 bits. If I don't do that I'll break protocol and my project group will kill me...

7. ## Re: Question: Bits and C++

oh no... if had to choose I wouldn't even do it XOR style... I would use a proper CRC16/32 checksum, the assignment requires me to use one char XOR checksum - 8 bits. If I don't do that I'll break protocol and my project group will kill me...
Well then my suggestion should be fitting.

8. ## Re: Question: Bits and C++

Well then my suggestion should be fitting.
Oh I am using a char, sorry for the mix up.

doh did I just write int in my code ex..... stupid me...

9. ## Re: Question: Bits and C++

Just a question, ive had some C++ but what do you mean by XORing a byte not bit by bit? How do you XOR a whole byte? Isnt it essentially doing it bit by bit no matter what?

10. ## Re: Question: Bits and C++

Just a question, ive had some C++ but what do you mean by XORing a byte not bit by bit? How do you XOR a whole byte? Isnt it essentially doing it bit by bit no matter what?
No worries, seems I got the protocol design wrong, my friend corrected it and I'll post his code tomorrow. He's code is much cleaner than mine, but I suspect he had help from our local C++ guru... Which really is cheating (when I do it it's clever... )

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•