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. Advertising

  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 http://derkgwen.250free.com/html/index.html
    Quit killing people. That's high profile.
     
    Derk Gwen, Jul 13, 2003
    #2
    1. Advertising

  3. Abby

    Malcolm Guest

    "Abby" <> wrote in message
    >
    > I have an array which contain Hex no. in each position.
    >
    > 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.
    >

    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. Emmanuel Delahaye <> scribbled the following:
    > In 'comp.lang.c', (Abby) wrote:
    >> 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,


    > How is this a C question?


    > "Add 1 and invert the bits."


    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.

    >> and how can I write the code for it.
    >> I'm still a newbie. Please advise. Thank you.


    > Do your best, and we will check your code.


    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

    Joona I Palaste wrote:
    >
    > Emmanuel Delahaye <> scribbled the following:
    > > In 'comp.lang.c', (Abby) wrote:
    > >> 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,

    >
    > > How is this a C question?

    >
    > > "Add 1 and invert the bits."

    >
    > No. "Invert the bits and add 1".


    Subtract 1 and invert the bits, also works.

    --
    pete
     
    pete, Jul 13, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve
    Replies:
    3
    Views:
    16,357
    Roedy Green
    May 7, 2006
  2. Mantorok Redgormor

    sign magnitude, ones complement, two's complement

    Mantorok Redgormor, Oct 5, 2003, in forum: C Programming
    Replies:
    8
    Views:
    8,608
    Glen Herrmannsfeldt
    Oct 8, 2003
  3. sarathy

    1's complement and 2's complement

    sarathy, Aug 1, 2006, in forum: C Programming
    Replies:
    20
    Views:
    2,195
    Bo Persson
    Aug 2, 2006
  4. sarathy
    Replies:
    22
    Views:
    2,354
    Bo Persson
    Aug 2, 2006
  5. Roberto Waltman

    2's complement vs. 1's complement vs. ...

    Roberto Waltman, Jun 9, 2011, in forum: C Programming
    Replies:
    4
    Views:
    1,359
    Michael Press
    Jun 14, 2011
Loading...

Share This Page