Thread: CheckSum help

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    5

    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?

    Many thanks in advance~

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    5
    hi,

    would this be correct ?

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

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No. >>8 and /8 are not the same thing.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    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. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    5
    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. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by remoir View Post
    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. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    5
    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. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > Is the below possible?
    No for the reason I mentioned above.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by zacs7 View Post
    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. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by remoir View Post
    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.
    Last edited by tabstop; 02-01-2009 at 10:22 PM.

  14. #14
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > (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).
    Last edited by zacs7; 02-01-2009 at 10:27 PM. Reason: Fix formatting

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. checksum sender vs reciever?
    By silhoutte75 in forum C Programming
    Replies: 1
    Last Post: 04-13-2008, 01:27 PM
  2. weird checksum function
    By sagitt13 in forum C Programming
    Replies: 7
    Last Post: 10-31-2006, 01:25 AM
  3. Help figuring out a checksum algorithm
    By barem23 in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 01-11-2003, 04:24 AM
  4. Reading Binary file to find Checksum
    By Abbila in forum C++ Programming
    Replies: 0
    Last Post: 09-25-2002, 09:52 PM

Tags for this Thread