binary addition of 2 bytes
binary addition of 2 bytes
(OP)
dear forum,
I need to implement a simple routine that performs binary addition of 2 bytes (stored as 'char') (the method is used for calculating checksum for an wireless comm)
e.g.
char a = '0x41';
char b = '0x52';
'0x41' 1000 0001
+'0x52' 0101 0010
='0xD3 1101 0011
does anyone have some sample code available of any links.
best regards
doneirik
I need to implement a simple routine that performs binary addition of 2 bytes (stored as 'char') (the method is used for calculating checksum for an wireless comm)
e.g.
char a = '0x41';
char b = '0x52';
'0x41' 1000 0001
+'0x52' 0101 0010
='0xD3 1101 0011
does anyone have some sample code available of any links.
best regards
doneirik





RE: binary addition of 2 bytes
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
RE: binary addition of 2 bytes
TTFN
RE: binary addition of 2 bytes
main(){
char a,b,c;
unsigned char d;
a = 0x41;
b = 0x52;
c= a + b;
d= (unsigned char) a + (unsigned char) b;
printf( "c=0x%x\td=0x%x\n",(char)c,d);
}
[richs@b52bdhcp1 junk]$ vi junk.c
[richs@b52bdhcp1 junk]$ !cc
cc junk.c
[richs@b52bdhcp1 junk]$ !./
./a.out
c=0xffffff93 d=0x93
That what you are looking for?
Cheers,
Rich S.
RE: binary addition of 2 bytes
Dan
Owner
http://www.Hi-TecDesigns.com
RE: binary addition of 2 bytes
sorry...I was too fast...
I´m programming in Visual C++ .Net 2003.
Any C code will do...
regards
eirik
RE: binary addition of 2 bytes
richs
has already given you the answer, although you might have an overange problem and might want to store your answer as a uint16. If you are developing in .NET and plan to use a small uP target, you might want to check the sign assumption of your target char, or better yet explicitly use signed char (or unsigned char) in all places.
RE: binary addition of 2 bytes
How did you get 0xD3 in your original query?
Do you need to mask out the top bit and set a parity bit accordingly?