Add two bit strings Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 11 Likes Like Report Given two binary strings s1 and s2 consisting of only 0s and 1s. Find the resultant string after adding the two Binary Strings.Note: The input strings may contain leading zeros but the output string should not have any leading zeros.Examples:Input: s1 = "1101", s2 = "111"Output: 10100Explanation: "1101" and "111" in decimal representation will be 13 and 7 respectively. Adding both the numbers gives 20, and its binary representation is "10100".Input: s1 = "00100", s2 = "010"Output: 110Explanation: "00100" and "010" in decimal representation will be 4 and 2 respectively. Adding both the numbers gives 6, and its binary representation is "110".This problem can be solved using Bit-by-bit addition with carry in O(n + m) Time and O(1) Space, The idea is to first trim the leading zeros in the input strings. Now, start from the last characters of the strings and compute the digit sum one by one. If the sum becomes more than 1, then store carry for the next digits. Also consider this carry while calculating the digit sum. After calculating the sum, if an additional carry is generated, prepend a ‘1’ of the result.Please refer Add two binary strings for detailed explanation and solution. Create Quiz Comment K kartik Follow 11 Improve K kartik Follow 11 Improve Article Tags : Bit Magic DSA binary-string binary-representation Bitwise-XOR +1 More Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 3 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 15 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 1 min read Problem of The Day - Develop the Habit of Coding 5 min read Like