Binary using Char

I'm trying to find the best way to do binary addition and subtraction with Char's.  

so say I've got:
char * a = '0001'
char * b = '0010'

I want it to return a char * that is the addition and subtraction of that number.

I've been simply converting it to decimal, and doing the math then converting back, but is there a better way?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenData Warehouse Architect / DBACommented:
Hi cfans,

What you're doing is fine, as long as the value(s) will all fit in one of the native integer types (char, int, long, long long).

If you expect data that is larger than the system can handle, you'll have to walk through the strings yourself adding the bits and keeping track of the carries.

Good Luck!
If I assume a & b to be always positive.

you can use a flow like:

// keep adding till one of your input attay is out of chars
remainder = get remainder that u get when you add a, b and prevRemainder

sum[count] = convert a, b and remainder to int. add a, b and remainder. convert to ascii.

prevRemainder = remainder

// atleast one of the inputs has hit its end of array.
// keep copying the other input's chars one by one. Don't forget the remainder!

sum[count] =  convert (a or b) and remainder to int. add them. convert to ascii.

be cautious with ascii to int, int to ascii conversions. a '1' is not same as value 1.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
What is the length of your input ? How would you represent negative numbers ?

an addition algorithm is very simple in one pass over the data.
if you'll use Vikram_B proposal, I would use a simple tabe

a b Reminder  -> Sum Reminder
0 0 0                  0      0
0 0 1                  1      0
0 1 0                  1      0
0 1 1                  0      1
1 0 0                  1      0
1 0 1                  0      1
1 1 0                  0      1
1 1 1                  1      1

or a few bit operations to have the same results...
deepu chandranCommented:
Currently u are doing decimel addisions and subtractions.That is better way i can find some other ways but it is not faster.

i doing like this just accept the binary type data as input and use one binary to decimal converter and manipulate. then convert back using decimel to binary converter function
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.