Binary using Char

Posted on 2006-03-22
Last Modified: 2013-11-15
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?
Question by:cfans
    LVL 45

    Expert Comment

    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!

    Accepted Solution

    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.

    LVL 2

    Expert Comment

    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.
    LVL 2

    Assisted Solution

    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...
    LVL 8

    Expert Comment

    by:deepu chandran
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Title # Comments Views Activity
    SAP PM 5 62
    Deep Freeze 2 58
    Line meaning 9 56
    Free software for pdf to word conversion 4 23
    This article shows how to convert a multi-page PDF file into multiple image files, with one image file created for each page of the PDF. It does this by utilizing an excellent, free software package called GraphicsMagick. The solution is amazingly s…
    Healthcare organizations in the United States must adhere to the guidance of both the HIPAA (Health Insurance Portability and Accountability Act) and HITECH (Health Information Technology for Economic and Clinical Health Act) for securing and protec…
    The viewer will learn how to back up with the free utility from runtime software, DriveImageXML using Windows 8. Download DriveImageXML from Open folder where it was saved: Start installation by double clicking the install scrip…
    The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now