How to calculate 2's complement 8 bits checksum ?

A

Abby

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

Derk Gwen

(e-mail address 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,
(unsigned char)((~data)+1),
(unsigned char)(-data));
return 0;
}

00 -> 00 (00)
01 -> ff (ff)
02 -> fe (fe)
0e -> f2 (f2)
 
M

Malcolm

Abby said:
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.
 
J

Joona I Palaste

Emmanuel Delahaye said:
In said:
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.
Do your best, and we will check your code.

Certainly his best can be better than your best... =)

--
/-- Joona Palaste ([email protected]) ---------------------------\
| 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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top