Add n binary strings in C++?



We are given n binary strings, and we need to add them together and return the result (as a binary string). A binary string is a string that consists only of the characters '0' and '1', which represent binary digits. We will add them bit by bit using binary addition rules.

Addition of Binary Numbers

Here's how binary addition with binary numbers works:

  • 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 look at a few example scenarios to understand it clearly:

Scenario 1

Input: ["101", "11", "1"]
Output: 1001
Explanation:
Let's align the binary numbers:
  101  
  011  (padded)
+ 001  (padded)
-----
 1001
We add bit by bit from right to left, keeping track of the carry.

Scenario 2

Input: ["111", "1", "10"]
Output: 1000
Explanation:
  111  
  001  (padded)
+ 010  (padded)
-----
 1000
Again, we add bits one by one, handle carries, and finally get the result.

Adding n Binary Strings

To add n binary strings, we need to apply the binary addition rules (discussed above) on their bits (0s and 1s) and return the final result as a binary string.

Algorithm

The following is the algorithm to add n binary strings:

  • First, we store the first binary string in a result variable and loop through the remaining binary strings one by one.
  • To add two binary strings, we have defined a helper function addBinary(), which first pads zeros to make both strings of equal length.
  • Then, we add the bits from right to left by converting characters to integers and keeping track of the carry at each step and building the result string accordingly.
  • After each addition, we update the result variable with the new binary sum returned from the addBinary() function.
  • After all binary strings are processed, the result contains the final binary sum.

C++ Program to Add n Binary Strings

The following is a C++ program where we add multiple binary strings by directly performing binary addition on their bits.

#include<iostream>
#include<algorithm>
using namespace std;

// Adds two binary strings and returns the sum in binary
string addBinary(string a, string b) {
   // Pad shorter string with leading 0s
   int len1 = a.length(), len2 = b.length();
   if (len1 < len2) a.insert(0, len2 - len1, '0');
   else if (len2 < len1) b.insert(0, len1 - len2, '0');
   
   string result = "";
   int carry = 0;
   
   // Add bits from right to left
   for (int i = a.length() - 1; i >= 0; i--) {
      int bit1 = a[i] - '0';
      int bit2 = b[i] - '0';
      int sum = bit1 + bit2 + carry;
      result = char(sum % 2 + '0') + result;
      carry = sum / 2;
   }
   // Add carry if left
   if (carry) result = '1' + result;
   return result;
}

// Adds all binary strings one by one
string addNBinaryStrings(string arr[], int n) {
   string result = arr[0];
   for (int i = 1; i < n; i++) {
      result = addBinary(result, arr[i]);
   }
   return result;
}

int main() {
   string arr[] = {"1011", "10", "1001"};
   int n = sizeof(arr) / sizeof(arr[0]);
   
   cout << "Input Binary Strings: ";
   for (int i = 0; i < n; i++) {
      cout << arr[i] << " ";
   }
   
   cout << "\nFinal Binary Sum: " << addNBinaryStrings(arr, n) << endl;
   return 0;
}

Following is the output of the above program, which displays the sum of all binary strings:

Input Binary Strings: 1011 10 1001 
Final Binary Sum: 10110

Time Complexity: O(n*m) because we add n strings and each addition takes up to m steps, where m is the max string length.

Space Complexity: O(m) because we store the result string, which can be up to m in length.

Conclusion

In this article, we saw how to add multiple binary strings in C++ by directly working with their bits. It's a simple method with a time complexity of O(n * m) and space complexity of O(m), where n is the number of strings and m is the maximum string length.

Updated on: 2025-07-29T18:52:05+05:30

261 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements