# CheckSum help

• 02-01-2009
remoir
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?

• 02-01-2009
tabstop
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.
• 02-01-2009
remoir
hi,

would this be correct ?

chksum = chksum + (int)(ReqMsg[i] & 0x00FF) + (int) ((ReqMsg[i]>>8) & 0x00FF) ;
• 02-01-2009
tabstop
No. >>8 and /8 are not the same thing.
• 02-01-2009
remoir
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) ;
• 02-01-2009
tabstop
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.
• 02-01-2009
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 ?
• 02-01-2009
tabstop
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.)
• 02-01-2009
zacs7
Quote:

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.
• 02-01-2009
remoir
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
• 02-01-2009
zacs7
> Is the below possible?
No for the reason I mentioned above.
• 02-01-2009
tabstop
Quote:

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.)
• 02-01-2009
tabstop
Quote:

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.
• 02-01-2009
zacs7
> (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).