![]() |
| ||
| Binary addition in C Hi there, I tried binary addition. It seems to work fine. Could someone tell me if there's a better way of doin this or any bugs that could be uncovered in the code below? I appreciate your time. Thanks, Tina #include <stdio.h> |
| ||
| Re: Binary addition in C why so complicated? all you really need is "strtol()" and the addition operator. int binaryAdd(char *first, char *second, int *sum) (The rest is just decoration) I'll leave conversion of the numeric sum, back into a binary string, as an exercise to the reader :P . |
| ||
| Re: Binary addition in C Hi, Thanks for responding! I was thinking of doing it at the bit level using the bit operators. Thanks again, Tina |
| ||
| Re: Binary addition in C at the "bit level", every integer is inherently binary and hex and decimal... it does not matter, because there's only a difference when you print it. so, you can make it hard if you like, just for the sake of having a challenge or if your instructor requires you to do so for learning purposes. but if you're not required to make it difficult, i'd say use the <stdlib.h> standard library functions whenever possible. |
| ||
| Re: Binary addition in C Well, this isn't at the bitlevel, you are interpreting the ascii values '0' '1' as bit 0 and bit 1. If you are interested in this bitlevel stuff, you should look into the shift operations. like unsigned char x=7; But it looks nice your code, one thing though, you don't need if (carry == 1) A good exercise is to write you addition program with a non fixed base like, |
| All times are GMT -4. The time now is 2:07 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC