Program to add two binary strings in C++



In this problem, we are given two binary strings, and we need to find their sum and return the result as a binary string. A binary string is a string that contains only the characters '0' and '1', where 0 and 1 are binary numbers.

While adding two binary numbers, we follow the binary addition rules given below -

  • 1 + 0 = 1
  • 0 + 1 = 1
  • 0 + 0 = 0
  • 1 + 1 = 0 with a carry of 1

When there is a carry, we add it to the next bit. For example, if the carry is 1, then adding 1 + 1 + carry (1) gives 1 with another carry of 1. Let's see some examples of adding two binary numbers:

Scenario 1

binary addition

Scenario 2

Input: str1 = {"110"}, str2 = {"1"}
Output: "111"
Explanation:
Adding binary strings from right to left with carry:
0 + 1 = 1, carry 0
1 + 0 + carry 0 = 1, carry 0
1 + 0 + carry 0 = 1, carry 0
Final sum = "111" 

Steps to Add Two Binary Strings

To add two binary strings in C++, we use a while loop and add digits until both strings are fully read and there's no carry left. The following are the steps:

  • Initialize two pointers to point to the last index of each binary string and set a variable carry to 0.
  • Create an empty string to store the final result.
  • Using a while loop as long as at least one pointer is valid or the carry is 1, convert each digit from the string to an integer, and add it to the carry. Then, calculate the sum % 2 to get the current bit and add it to the front of the result string. Finally, update the carry using sum / 2 and move both pointers one step to the left.
  • When the loop ends, return the result string, which holds the sum of the two binary strings.

Example

Below is a C++ program where we follow the above steps to add two binary strings and get the final result:

#include <iostream>
using namespace std;

// Function to add two binary strings
string addBinary(string a, string b) {
   string result = "";
   int size_a = a.size() - 1;
   int size_b = b.size() - 1;
   int carry = 0;
   // Run the loop until both pointers are less than 0 and carry is 0
   while (size_a >= 0 || size_b >= 0 || carry == 1) {
      int sum = carry;
      if (size_a >= 0) {
         sum += a[size_a] - '0';
         size_a--;
      }
      if (size_b >= 0) {
         sum += b[size_b] - '0';
         size_b--;
      }
      result = char(sum % 2 + '0') + result;
      carry = sum / 2;
   }
   return result;
}
int main() {
   string a = "11010";
   string b = "1101";
   string sum = addBinary(a, b);

   cout << "First binary string: " << a << endl;
   cout << "Second binary string: " << b << endl;
   cout << "Sum of binary strings: " << sum << endl;
   return 0;
}

When we run the above program, we get the following output:

First binary string: 11010
Second binary string: 1101
Sum of binary strings: 100111

Time Complexity: O(max(N, M)) because we check each digit of both strings once.

Space Complexity: O(max(N, M)) because we make a new string to store the result.

Conclusion

In this article, we learned to add two binary strings in C++ using a while loop. The method is simple and handles the carry during addition. The program runs with time and space complexity of O(n), where n is the length of the longer string.

Updated on: 2025-07-23T18:01:13+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements