×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

binary addition of 2 bytes

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

RE: binary addition of 2 bytes

Lots of ways, but the first question is in what language?

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein

RE: binary addition of 2 bytes

Don't what language you're using, but maybe your problem has to do with equating 0x41 with 1000 0001, when my recollection of binary results in 0x41 = 0100 0001?

TTFN



RE: binary addition of 2 bytes

Fedora 2 platform (linux)

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

Am I missing the point of the question?  Asking how to add two numbers in ANY language seems WAY too simplistic...

Dan
Owner
http://www.Hi-TecDesigns.com

RE: binary addition of 2 bytes

(OP)
hi again....

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

You mentioned "for a checksum".  You may want to make sure you do not normal additioin as discussed above.  You may want to be adding several numbers and do not want to perform any carry operations.  If you do not have to worry about carries, and are allowed to carry, then
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

0x41 + 0x52 = 0x93.

How did you get 0xD3 in your original query?

Do you need to mask out the top bit and set a parity bit accordingly?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources