Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8810037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:01:32+00:00 2026-06-14T03:01:32+00:00

How would I add two binary numbers in C++? What is the correct logic?

  • 0

How would I add two binary numbers in C++? What is the correct logic?

Here is my effort, but it doesn’t seem to be correct:

#include <iostream>
using namespace std;
int main()
{
    int a[3];
    int b[3];
    int carry = 0;
    int result[7];

    a[0] = 1;
    a[1] = 0;
    a[2] = 0;
    a[3] = 1;

    b[0] = 1;
    b[1] = 1;
    b[2] = 1;
    b[3] = 1;

    for(int i = 0; i <= 3; i++)
    {
        if(a[i] + b[i] + carry == 0)
        {
            result[i] = 0;
            carry = 0;
        }

        if(a[i] + b[i] + carry == 1)
        {
            result[i] = 0;
            carry = 0;
        }

        if(a[i] + b[i] + carry == 2)
        {
            result[i] = 0;
            carry = 1;
        }

        if(a[i] + b[i] + carry > 2)
        {
            result[i] = 1;
            carry = 1;
        }
    }
    for(int j = 0; j <= 7; j++)
    {
        cout<<result[j]<<" ";
    }
    system("pause");
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:01:33+00:00Added an answer on June 14, 2026 at 3:01 am

    Well, it is a pretty trivial problem.

    How to add two binary numbers in c++. what is the logic of it.

    For adding two binary numbers, a and b. You can use the following equations to do so.

    sum = a xor b

    carry = ab

    This is the equation for a Half Adder.

    Now to implement this, you may need to understand how a Full Adder works.

    sum = a xor b xor c

    carry = ab+bc+ca

    Since you store your binary numbers in int array, you might want to understand bitwise operation.
    You can use ^ for XOR,| operator for OR, & operator for AND.

    Here is a sample code to calculate the sum.

    for(i = 0; i < 8 ; i++){
       sum[i] = ((a[i] ^ b[i]) ^ c); // c is carry
       c = ((a[i] & b[i]) | (a[i] & c)) | (b[i] & c); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add two numbers together but when one of those numbers is
I am trying to figure out how to add two binary numbers together which
How would I add together the values pulled from two separate jQuery.html() calls? Example:
I have two doubts: 1. I would like to add some client side support
I'm using Elmah with ASP.NET and wondering how I would add custom data, such
I don't understand how using a 'challenge token' would add any sort of prevention:
The title speaks for itself, but I would add some pointers I've noticed along
I need to add two simple polynomials (represented as strings). The following example would
I would like to add two compiler flags, one for ARC (-fno-objc-arc) and one
How do I add two numbers together when I don't know their type in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.