How to calculate 2's complement 8 bits checksum ?

Discussion in 'C Programming' started by Abby, Jul 13, 2003.

  1. Abby

    Abby Guest

    I have an array which contain Hex no. in each position.

    For examples,
    unsigned char data[5];

    data[0] = 0x00;
    data[1] = 0x01;
    data[2] = 0x02;
    data[3] = 0xE;
    data[4] = 0xEF; --> This is the checksum value

    checksum value at data[4] is the checksum calculated from data[0] -
    data[3]. I would like to know what is the algorithm to do the 2's
    complement for 8 bits checksum, and how can I write the code for it.
    I'm still a newbie. Please advise. Thank you.
     
    Abby, Jul 13, 2003
    #1
    1. Advertisements

  2. Abby

    Derk Gwen Guest

    (Abby) wrote:
    # I have an array which contain Hex no. in each position.
    #
    # For examples,
    # unsigned char data[5];
    #
    # data[0] = 0x00;
    # data[1] = 0x01;
    # data[2] = 0x02;
    # data[3] = 0xE;
    # data[4] = 0xEF; --> This is the checksum value
    #
    # checksum value at data[4] is the checksum calculated from data[0] -
    # data[3]. I would like to know what is the algorithm to do the 2's
    # complement for 8 bits checksum, and how can I write the code for it.
    # I'm still a newbie. Please advise. Thank you.

    One's complement is just complement: ~x.
    Two's complement is complement and increment, ignoring carry: (~x)+1.

    #include <stdio.h>
    int main(int N,char **P) {
    static unsigned char data[5] = {0x00,0x01,0x02,0x0E,0};
    int i;
    for (i=0; i<4; i++) printf("%02x -> %02x (%02x)\n",data,
    (unsigned char)((~data)+1),
    (unsigned char)(-data));
    return 0;
    }

    00 -> 00 (00)
    01 -> ff (ff)
    02 -> fe (fe)
    0e -> f2 (f2)
     
    Derk Gwen, Jul 13, 2003
    #2
    1. Advertisements

  3. Abby

    Malcolm Guest

    Two's complement is a way of representing negative numbers in binary.
    Instead of having a bit which acts as a negative "flag", we invert, and then
    increment.
    So 1 is 0000 0001 int binary
    inverting gives 1111 1110
    incrementing gives 1111 1111 or -1 in two's complement.

    The nice thing is that by adding, and discarding the overflow, we get the
    correct answer.

    0000 0001 = 1
    +1111 1111 = -1
    =1 0000 0000

    discard the overflow and we get 1 + -1 = 0;

    A checksum is a technique to check data for transmission errors or
    tampering. If the last few bytes are the sum of all the preceding bytes,
    then any errors are likely to be detected.

    The problem is that "two's complement checksum" doesn't have a definite
    meaning that I'm aware of. It obviously has something to do with taking the
    two's complement of a number somewhere, and presumably each byte contributes
    to the final answer, but you will have to ask further what exactly is meant
    by the term.
     
    Malcolm, Jul 13, 2003
    #3
  4. No. "Invert the bits and add 1". If you do it the other way around,
    you end up with the wrong result. Let's use eight-bit values for
    simplicity. The 2's complement of 0 (binary 00000000) should also be 0.
    First we add 1 (result binary 00000001) and then we invert the bits:
    result binary 11111110. If we do it the right way, we first invert the
    bits (result binary 11111111) and then add 1: result binary 00000000,
    due to a wrap-around.
    Certainly his best can be better than your best... =)

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "As we all know, the hardware for the PC is great, but the software sucks."
    - Petro Tyschtschenko
     
    Joona I Palaste, Jul 13, 2003
    #4
  5. Abby

    pete Guest

    Subtract 1 and invert the bits, also works.
     
    pete, Jul 13, 2003
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.