By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,939 Members | 1,602 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,939 IT Pros & Developers. It's quick & easy.

How to calculate 2's complement 8 bits checksum ?

P: n/a
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.
Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
ab*****@yahoo.com (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.
Nov 13 '05 #2

P: n/a

"Abby" <ab*****@yahoo.com> 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.
Nov 13 '05 #3

P: n/a
Emmanuel Delahaye <em**********@noos.fr> scribbled the following:
In 'comp.lang.c', ab*****@yahoo.com (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 (pa*****@cc.helsinki.fi) ---------------------------\
| 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
Nov 13 '05 #4

P: n/a
Joona I Palaste wrote:

Emmanuel Delahaye <em**********@noos.fr> scribbled the following:
In 'comp.lang.c', ab*****@yahoo.com (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
Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.