Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to calculate 2's complement 8 bits checksum ?

Reply
Thread Tools

How to calculate 2's complement 8 bits checksum ?

 
 
Abby
Guest
Posts: n/a
 
      07-13-2003
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.
 
Reply With Quote
 
 
 
 
Derk Gwen
Guest
Posts: n/a
 
      07-13-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (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[i],
(unsigned char)((~data[i])+1),
(unsigned char)(-data[i]));
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.
 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      07-13-2003

"Abby" <(E-Mail Removed)> 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.


 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      07-13-2003
Emmanuel Delahaye <(E-Mail Removed)> scribbled the following:
> In 'comp.lang.c', (E-Mail Removed) (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 ((E-Mail Removed)) ---------------------------\
| 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
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      07-13-2003
Joona I Palaste wrote:
>
> Emmanuel Delahaye <(E-Mail Removed)> scribbled the following:
> > In 'comp.lang.c', (E-Mail Removed) (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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
2's complement vs. 1's complement vs. ... Roberto Waltman C Programming 4 06-13-2011 11:26 PM
1's complement and 2's complement sarathy C++ 22 08-02-2006 05:53 PM
1's complement and 2's complement sarathy C Programming 20 08-02-2006 05:53 PM
sign magnitude, ones complement, two's complement Mantorok Redgormor C Programming 8 10-07-2003 11:52 PM



Advertisments