Click here to Skip to main content
14,924,426 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
This is my code for addition of two binary numbers. There is no error but the output is not correct. Please rectify this code only. I am not looking for new code.

What I have tried:

#include <bits/stdc++.h>
using namespace std;
int reverse(int n)
{
    int ans = 0;
    while (n > 0)
    {
        int lastdigit = n % 10;
        ans = ans * 10 + lastdigit;
        n /= 10;
    }
    return ans;
}
int addbinary(int a, int b)
{
    int ans = 0;
    int prevcarry = 0;
    while (a > 0 && b > 0)
    {
        if (a % 2 == 0 && b % 2 == 0)
        {
            ans = ans * 10 + prevcarry;
            prevcarry = 0;
        }
        else if ((a % 2 == 0 && b % 2 == 1) || (a % 2 == 1 && b % 2 == 0))
        {
            if (prevcarry = 1)
            {
                ans = ans * 10 + 0;
                prevcarry = 1;
            }
            else
            {
                ans = ans * 10 + 1;
                prevcarry = 0;
            }
        }
        else
        {
            ans = ans * 10 + prevcarry;
            prevcarry = 1;
        }
        a /= 10;
        b /= 10;
    }
    while (a > 0)
    {
        if (prevcarry == 1)
        {
            if (a % 2 == 1)
            {
                ans = ans * 10 + 0;
                prevcarry = 1;
            }

            else
            {
                ans = ans * 10 + 1;
                prevcarry = 0;
            }
        }
        else
        {
            ans = ans * 10 + (a % 2);
        }
        a /= 10;
    }
    while (b > 0)
    {
        if (prevcarry == 1)
        {
            if (b % 2 == 1)
            {
                ans = ans * 10 + 0;
                prevcarry = 1;
            }

            else
            {
                ans = ans * 10 + 1;
                prevcarry = 0;
            }
        }
        else
        {
            ans = ans * 10 + (b % 2);
        }
        b /= 10;
    }
    if (prevcarry == 1)
    {
        ans = ans * 10 + 1;
    }
    ans = reverse(ans);
    return ans;
}

int32_t main()
{
    int a, b;
    cin >> a >> b;
    cout << addbinary(a, b) << endl;
    return 0;
}
Posted
Updated 31-Dec-20 8:05am

Quote:
There is no error but the output is not correct.
On the contrary, there is a major error. Every time you add a digit to the answer, you first multiply the answer by 10. But you are doing binary arithmetic, not decimal, so you should multiply by 2.

[edit]
You could improve the code by using a #define for your base radix rather than hard coding the numbers.
[/edit]
   
v2
Comments
Stefan_Lang 1-Jan-21 12:21pm
   
Well spotted, have a 5.

Although I'd suggest a const int for the radix rather than a #define - after all this is C++, not C. (note: not unsigned int - a negative radix is actually a valid option ;-) )
Richard MacCutchan 1-Jan-21 12:28pm
   
Thanks.
Well it's C++ in that it uses cin and cout, but apart from that ... :)
Quote:
There is no error but the output is not correct.


Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
   
Quote:
Please rectify this code only. I am not looking for new code.

What about discovering by yourself what is the problem by using proper tool, the debugger.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900