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 6567109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:17:49+00:00 2026-05-25T14:17:49+00:00

I am making a program that adds two binary numbers (up to 31 digits)

  • 0

I am making a program that adds two binary numbers (up to 31 digits) together and outputs the sum in binary.

I have every thing working great but I need to remove the leading zeros off the solution.

This is what my output is:

char c[32];
int carry = 0;
if(carry == '1')
{
    cout << carry;
}

for(i = 0; i < 32; i++)
{
    cout << c[i]; 
}

I tried this but it didn’t work:

char c[32];
int carry = 0;
bool flag = false;

if(carry == '1')
{
    cout << carry;
}

for(i=0; i<32; i++)
{
    if(c[i] != 0)
    {
        flag = true;

        if(flag)
        {
            for(i = 0; i < 32; i++)
            {
                cout << c[i]; 
            }
        }
    }
}

Any ideas or suggestions would be appreciated.

EDIT: Thank you everyone for your input, I got it to work!

  • 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-05-25T14:17:51+00:00Added an answer on May 25, 2026 at 2:17 pm

    You should not have that inner loop (inside if(flag)). It interferes with the i processing of the outer loop.

    All you want to do at that point is to output the character if the flag is set.

    And on top of that, the printing of the bits should be outside the detection of the first bit.

    The following pseudo-code shows how I’d approach this:

    set printing to false
    if carry is 1:
        output '1:'
    
    for each bit position i:
        if c[i] is 1:
            set printing to true
        if printing:
            output c[i]
    
    if not printing:
        output 0
    

    The first block of code may need to be changed to accurately output the number with carry. For example, if you ended up with the value 2 and a carry, you would want either of:

    1:10                              (or some other separator)
    100000000000000000000000000000010 (33 digits)
    

    Simply outputting 110 with no indication that the leftmost bit was a carry could either be:

    • 2 with carry; or
    • 6 without carry

    The last block ensures you have some output for the value 0 which would otherwise print nothing since there were no 1 bits.

    I’ll leave it up to you whether you should output a separator between carry and value (and leave that line commented out) or use carry to force printing to true initially. The two options would be respectively:

    if carry is 1:
        output '1 '
    

    and:

    if carry is 1:
        output 1
        set printing to true
    

    And, since you’ve done the conversion to C++ in a comment, that should be okay. You state that it doesn’t work, but I typed in your code and it worked fine, outputting 10:

    #include <iostream>
    
    int main(void)
    {
        int i;
        int carry = 0;
        int c[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0};
        bool print = false;
    
        // This is the code you gave in the comment, slightly modified.
        // vvvvvv
        if(carry == 1) {
            std::cout << carry << ":";
        }
    
        for (i = 0; i < 32; i++) {
            if (c[i] == 1) {
                print = true;
            }
    
            if (print) {
                std::cout << c[i];
            }
        }
        // ^^^^^^
    
        std::cout << std::endl;
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently learning Windows/DOS assembly. I'm just making a small program that adds two
I was making a program that rounds up numbers with various decimal places, so
I am making a program that solves an equation. I have a variable that
I'm making a C program that needs to use two stacks. One needs to
I'm making a program that will have a widget that has to be fixed
I'm making a program that can recursively add Polynomials (represented by Linked Lists) together.
I am making a program that makes use of two Array Lists. I've researched
I'm making a program that adds executable files to a listBox in C#. I'm
I am making a program that generates a Javascript file. I have never worked
I'm making a program that fits the wizard concept ideally; the user is walked

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.