1. CheckSum help

Hi all,

i am trying to write a checksum function found in a PLC program.

i have a line as below

chksum = chksum + (ReqMsg[i] & 00FF) + ((floor(ReqMsg[i]/8)) & 00FF) ;

i want to evaluate the expression, but it contains a char ReqMsg[i] and a int chksum and 00FF. If i want the final evaluation to be integer, how should i change the line?

2. To get hex, you have to use 0x before it. You don't need floor, since that's how division works in the first place.

3. hi,

would this be correct ?

chksum = chksum + (int)(ReqMsg[i] & 0x00FF) + (int) ((ReqMsg[i]>>8) & 0x00FF) ;

4. No. >>8 and /8 are not the same thing.

5. i am hoping to do a shift right by 8 bits for ReqMsg[i] which is a char, and then taking that result AND 0x00FF, before finally converting the value to integer. Do i need to change the variable type for ReqMsg[i] before doing the bit shift ?

chksum = chksum + (int)(ReqMsg[i] & 0x00FF) + (int) ( ( ((int)ReqMsg[i]) >>8) & 0x00FF) ;

6. There only are eight bits in a char. If you shift eight bits away, you won't have anything left. Dividing by eight and shifting by eight aren't the same thing.

7. er. then can i AND a char ReqMSG with 0x00FF ? i am not sure if this is possible. Would the result be a char ?

8. It's possible, it just doesn't do anything. Unless you've got a PLC that has 16-bit chars (which seems highly unlikely) or your ReqMsg is actually an array of 16-bit short/int, I don't see how >>8 is going to do you any good. (I'll point it out one last time: your original post says /8 -- and dividing by 8 is shifting by 3, not shifting by 8.)

9. Originally Posted by remoir
er. then can i AND a char ReqMSG with 0x00FF ? i am not sure if this is possible. Would the result be a char ?
It should be an unsigned char since bitwise ops on signed types are undefined.

10. i apologize for the change in the code. I was originally intending to use >> 8 to shift it 8 bits over. You are right in that it had a 16-bit char per index in the ReqMsg array.

i googled http://irc.essex.ac.uk/www.iota-six...._operators.asp which said that effectively shifting 8 bits, is the same, as dividing the value by 2^8 , rounding the final value down. I am trying to follow the exact code used in the PLC program , to mirror it in C.

But, i am not very familiar with bitwise operations. Haven't used them before. I have some questions i need to answer.

Is the below possible?

char ReqMsg[] AND 0x00FF
(int ) ( char ReqMsg[] AND 0x00FF )
(~chksum) AND 0x00FF

11. > Is the below possible?
No for the reason I mentioned above.

12. Originally Posted by zacs7
It should be an unsigned char since bitwise ops on signed types are undefined.
You'd better quick tell the C committee that, then.

(Shifting signed things is undefined. Bitwise operations are well-defined on signed types.)

13. Originally Posted by remoir
Is the below possible?

char ReqMsg[] AND 0x00FF
(int ) ( char ReqMsg[] AND 0x00FF )
(~chksum) AND 0x00FF
Yes these are possible.

(edit) And for that matter, you do have a compiler, right? You could try some values out and see what happens.

14. > (Shifting signed things is undefined. Bitwise operations are well-defined on signed types.)
I *was* talking about the shift, specifically (signed char / int) ReqMsg[i] >> 8;

And according to the C standard "The results of bitwise operation on signed integers is implementation-defined" -- MSDN lists what the Microsoft compilers do http://msdn.microsoft.com/en-us/library/17zwb64t.aspx. Implementation-defined and "Undefined" are usually interchangable (take void main() or divide-by-zero for example).