XOR of two Binary Strings

30 Apr, 2021
Given two binary strings A and B of equal lengths, the task is to print a string which is the XOR of Binary Strings A and B.Examples:  
Input: A = “0001”, B = “0010” Output: 0011Input: A = “1010”, B = “0101” Output: 1111  
 
Approach: The idea is to iterate over both the string character by character and if the character mismatched then add “1” as the character in the answer string otherwise add “0” to the answer string to generate the XOR string. 
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ Implementation to find the// XOR of the two Binary Strings#include<bits/stdc++.h>using namespace std; // Function to find the// XOR of the two Binary Stringsstring xoring(string a, string b, int n){string ans = "";         // Loop to iterate over the    // Binary Strings    for (int i = 0; i < n; i++)    {        // If the Character matches        if (a[i] == b[i])            ans += "0";        else            ans += "1";    }    return ans;} // Driver Codeint main(){    string a = "1010";    string b = "1101";    int n = a.length();    string c = xoring(a, b, n);    cout << c << endl;} // This code is contributed by Surendra_Gangwar
// Java Implementation to find the// XOR of the two Binary Stringsimport java.io.*; class GFG {    // Function to find the    // XOR of the two Binary Strings    static String  xoring(String a, String b, int n){    String ans = "";                 // Loop to iterate over the        // Binary Strings        for (int i = 0; i < n; i++)        {            // If the Character matches            if (a.charAt(i) == b.charAt(i))                ans += "0";            else                ans += "1";        }        return ans;    }         // Driver Code    public static void main (String[] args)    {        String a = "1010";        String b = "1101";        int n = a.length();        String c = xoring(a, b, n);        System.out.println(c);    }} // This code is contributed by shubhamsingh10
# Python Implementation to find the# XOR of the two Binary Strings # Function to find the# XOR of the two Binary Stringsdef xor(a, b, n):    ans = ""         # Loop to iterate over the    # Binary Strings    for i in range(n):                 # If the Character matches        if (a[i] == b[i]):            ans += "0"        else:            ans += "1"    return ans # Driver Codeif __name__ == "__main__":    a = "1010"    b = "1101"    n = len(a)    c = xor(a, b, n)    print(c)
// C# Implementation to find the// XOR of the two Binary Stringsusing System; class GFG{    // Function to find the    // XOR of the two Binary Strings    static string xoring(string a, string b, int n){    string ans = "";                 // Loop to iterate over the        // Binary Strings        for (int i = 0; i < n; i++)        {            // If the Character matches            if (a[i] == b[i])                ans += "0";            else                ans += "1";        }        return ans;    }         // Driver Code    static public void Main ()    {        string a = "1010";        string b = "1101";        int n = a.Length;        string c = xoring(a, b, n);        Console.WriteLine(c);    }} // This code is contributed by shubhamsingh10
<script> // Javascript Implementation to find the// XOR of the two Binary Strings // Function to find the// XOR of the two Binary Stringsfunction xoring(a, b, n){let ans = "";         // Loop to iterate over the    // Binary Strings    for (let i = 0; i < n; i++)    {        // If the Character matches        if (a[i] == b[i])            ans += "0";        else            ans += "1";    }    return ans;} // Driver Code    let a = "1010";    let b = "1101";    let n = a.length;    let c = xoring(a, b, n);    document.write(c); </script>
Output:
0111
SURENDRA_GANGWAR
SHUBHAMSINGH10
rishavmahato348
Bitwise-XOR
Mathematical
Strings
Strings
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Prime Numbers
Program to find GCD or HCF of two numbers
Find minimum number of coins that make a given value
Write a program to reverse an array or string
Reverse a string in Java
Different Methods to Reverse a String in C++
Check for Balanced Brackets in an expression (well-formedness) using Stack
Longest Common Subsequence | DP-4