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 last characters of two strings and compute digit sum one by one. If sum becomes more than 1, then store carry for 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) { string result = ""; // Initialize result int s = 0; // Initialize digit sum // Traverse both strings starting from last // characters int i = a.size() - 1, j = b.size() - 1; while (i >= 0 || j >= 0 || s == 1) { // Comput sum of last digits and carry s += ((i >= 0)? a[i] - '0': 0); s += ((j >= 0)? b[j] - '0': 0); // If current digit sum is 1 or 3, add 1 to result result = char(s % 2 + '0') + result; // Compute carry s /= 2; // Move to next digits i--; j--; } return result; } // Driver program int main() { string a = "1101", b="100"; cout << addBinary(a, b) << endl; return 0; } |
chevron_right
filter_none
Java
// java program to add // two binary strings public class GFG { // This function adds two // binary strings and return // result as a third string static String addBinary(String a, String b) { // Initialize result String result = ""; // Initialize digit sum int s = 0; // Traverse both strings starting // from last characters int i = a.length() - 1, j = b.length() - 1; while (i >= 0 || j >= 0 || s == 1) { // Comput sum of last // digits and carry s += ((i >= 0)? a.charAt(i) - '0': 0); s += ((j >= 0)? b.charAt(j) - '0': 0); // If current digit sum is // 1 or 3, add 1 to result result = (char)(s % 2 + '0') + result; // Compute carry s /= 2; // Move to next digits i--; j--; } return result; } //Driver code public static void main(String args[]) { String a = "1101", b="100"; System.out.print(addBinary(a, b)); } } // This code is contributed by Sam007. |
chevron_right
filter_none
Python3
# Python Solution for above problem: # This function adds two binary # strings return the resulting string def add_binary_nums(x, y): max_len = max(len(x), len(y)) x = x.zfill(max_len) y = y.zfill(max_len) # initialize the result result = '' # initialize the carry carry = 0 # Traverse the string for i in range(max_len - 1, -1, -1): r = carry r += 1 if x[i] == '1' else 0 r += 1 if y[i] == '1' else 0 result = ('1' if r % 2 == 1 else '0') + result carry = 0 if r < 2 else 1 # Compute the carry. if carry !=0 : result = '1' + result return result.zfill(max_len) # Driver code print(add_binary_nums('1101', '100')) # This code is contributed # by Anand Khatri |
chevron_right
filter_none
C#
// C# program to add // two binary strings using System; class GFG { // This function adds two // binary strings and return // result as a third string static string addBinary(string a, string b) { // Initialize result string result = ""; // Initialize digit sum int s = 0; // Traverse both strings starting // from last characters int i = a.Length - 1, j = b.Length - 1; while (i >= 0 || j >= 0 || s == 1) { // Comput sum of last // digits and carry s += ((i >= 0)? a[i] - '0': 0); s += ((j >= 0)? b[j] - '0': 0); // If current digit sum is // 1 or 3, add 1 to result result = (char)(s % 2 + '0') + result; // Compute carry s /= 2; // Move to next digits i--; j--; } return result; } // Driver Code public static void Main() { string a = "1101", b="100"; Console.Write( addBinary(a, b)); } } // This code is contributed by Sam007 |
chevron_right
filter_none
PHP
<?php // PHP program to add two binary strings // This function adds two binary strings // and return result as a third string function addBinary($a, $b) { $result = ""; // Initialize result $s = 0; // Initialize digit sum // Traverse both strings starting // from last characters $i = strlen($a) - 1; $j = strlen($b) - 1; while ($i >= 0 || $j >= 0 || $s == 1) { // Comput sum of last digits and carry $s += (($i >= 0)? ord($a[$i]) - ord('0'): 0); $s += (($j >= 0)? ord($b[$j]) - ord('0'): 0); // If current digit sum is 1 or 3, // add 1 to result $result = chr($s % 2 + ord('0')) . $result; // Compute carry $s = (int)($s / 2); // Move to next digits $i--; $j--; } return $result; } // Driver Code $a = "1101"; $b = "100"; echo addBinary($a, $b); // This code is contributed by mits ?> |
chevron_right
filter_none
Output:
10001
Thanks to Gaurav Ahirwar for suggesting above solution. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Add n binary strings
- Add two numbers represented by two arrays
- Add two integers of different base and represent sum in smaller base of the two
- Pairs of complete strings in two sets of strings
- Meta Strings (Check if two strings can become same after a swap in one string)
- Number of common base strings for two strings
- Count of strings that become equal to one of the two strings after one removal
- Count of same length Strings that exists lexicographically in between two given Strings
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Program to add two fractions
- Program to add two polynomials
- Program to add two integers of given base
- Write a program to add two numbers in base 14
- Given two binary strings perform operation until B > 0 and print the result
- XOR two binary strings of unequal lengths
- XOR of two Binary Strings
- Count of distinct XORs formed by rearranging two Binary strings
- Python program to find uncommon words from two Strings
- Program to check if two strings are same or not
- C++ program to compare two Strings using Operator Overloading

