C++ Program To Add Two Binary Strings Last Updated : 17 Jan, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Try it on GfG Practice Given two binary strings, return their sum (also a binary string).Example: Input: a = "11", b = "1" Output: "100" We strongly recommend you to minimize your browser and try this yourself first The idea is to start from the last characters of two strings and compute the digit sum one by one. If the sum becomes more than 1, then store carry for the next digits. C++ // C++ program to add two // binary strings #include <bits/stdc++.h> using namespace std; // This function adds two binary strings // and return result as a third string string addBinary(string A, string B) { // If the length of string A is greater // than the length of B then just swap // the string by calling the same // function and make sure to return // the function otherwise recursion // will occur which leads to calling // the same function twice if (A.length() > B.length()) return addBinary(B, A); // Calculating the difference between // the length of the two strings. int diff = B.length() - A.length(); // Initialise the padding string which // is used to store zeroes that should // be added as prefix to the string which // has length smaller than the other string. string padding; for (int i = 0; i < diff; i++) padding.push_back('0'); A = padding + A; string res; char carry = '0'; for (int i = A.length() - 1; i >= 0; i--) { // This if condition solves 110 111 // possible cases if (A[i] == '1' && B[i] == '1') { if (carry == '1') res.push_back('1'), carry = '1'; else res.push_back('0'), carry = '1'; } // This if condition solves 000 001 // possible cases else if (A[i] == '0' && B[i] == '0') { if (carry == '1') res.push_back('1'), carry = '0'; else res.push_back('0'), carry = '0'; } // This if condition solves 100 101 010 // 011 possible cases else if (A[i] != B[i]) { if (carry == '1') res.push_back('0'), carry = '1'; else res.push_back('1'), carry = '0'; } } // If at the end their is carry then just // add it to the result if (carry == '1') res.push_back(carry); // reverse the result reverse(res.begin(), res.end()); // To remove leading zeroes int index = 0; while (index + 1 < res.length() && res[index] == '0') index++; return (res.substr(index)); } // Driver code int main() { string a = "1101", b = "100"; cout << addBinary(a, b) << endl; return 0; } Output: 10001 Time Complexity: O(max(L1, L2)), where L1 and L2 are the lengths of strings a and b respectively. Auxiliary Space: O(max(L1, L2)), where L1 and L2 are the lengths of strings a and b respectively. Comment More infoAdvertise with us Next Article std::string::append vs std::string::push_back() vs Operator += in C++ K kartik Follow Improve Article Tags : C++ CPP Strings Programs Practice Tags : CPP Similar Reads C++ <cstring> The <cstring> library is a part of the standard C++ library collection that provides the commonly used methods for C-Style string manipulation. It is inherited from the <string.h> library of C language. We can import the <cstring> header file using #include preprocessor directive a 5 min read std::string::append vs std::string::push_back() vs Operator += in C++ To append characters, you can use operator +=, append(), and push_back(). All of them helps to append character but with a little difference in implementation and application. Operator += : appends single-argument values. Time complexity : O(n)append() : lets you specify the appended value by using 6 min read std::string::append() in C++ The string::append() in C++ is used append the characters or the string at the end of the string. It is the member function of std::string class .The string::append() function can be used to do the following append operations:Table of ContentAppend a Whole StringAppend a Part of the StringAppend a C 3 min read Add two unsigned numbers using bits Given two unsigned integers (maximum possible input can be of 32 bits). The task is to add two numbers using bit operations. Examples: Input: n1 = 12, n2 = 34Output: 46Input: n1 = 12564 n2 = -1Output: 12563 Approach: Since we know that in bit addition 1+0=10+1=10+0=01+1=0 carry 1if(carry==1) 1+1=1 c 7 min read Minimum subsequences of a string A required to be appended to obtain the string B Given two strings A and B, the task is to count the minimum number of operations required to construct the string B by following operations: Select a subsequence of the string A.Append the subsequence at the newly formed string (initially empty). Print the minimum count of operations required. If it 8 min read What happen if we concatenate two string literals in C++? If you are going to perform concatenation in C++, some of the things you must be kept in mind are: If a+b is an expression that is showing string concatenation, then the result of the expression will be a copy of the character in 'a' followed by the character in 'b'.Either 'a' or 'b' can be string l 2 min read Like