XOR two binary strings of unequal lengths

06 Jul, 2021
Given two binary string of unequal lengths A and B, the task is to print the binary string which is the XOR of A and B.Examples: 
Input: A = “11001”, B = “111111” Output: 100110Input: A = “11111”, B = “0” Output: 11111 
Approach: The idea is to first make both the strings of equal length and then perform the XOR of each character one by one and store it in the resultant string.Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to insert n 0s in the// beginning of the given stringvoid addZeros(string& str, int n){    for (int i = 0; i < n; i++) {        str = "0" + str;    }} // Function to return the XOR// of the given stringsstring getXOR(string a, string b){     // Lengths of the given strings    int aLen = a.length();    int bLen = b.length();     // Make both the strings of equal lengths    // by inserting 0s in the beginning    if (aLen > bLen) {        addZeros(b, aLen - bLen);    }    else if (bLen > aLen) {        addZeros(a, bLen - aLen);    }     // Updated length    int len = max(aLen, bLen);     // To store the resultant XOR    string res = "";    for (int i = 0; i < len; i++) {        if (a[i] == b[i])            res += "0";        else            res += "1";    }     return res;} // Driver codeint main(){    string a = "11001", b = "111111";     cout << getXOR(a, b);     return 0;}
// Java implementation of the approachclass GFG{         // Function to insert n 0s in the    // beginning of the given string    static String addZeros(String str, int n)    {        for (int i = 0; i < n; i++)        {            str = "0" + str;        }        return str;    }         // Function to return the XOR    // of the given strings    static String getXOR(String a, String b)    {             // Lengths of the given strings        int aLen = a.length();        int bLen = b.length();             // Make both the strings of equal lengths        // by inserting 0s in the beginning        if (aLen > bLen)        {            a = addZeros(b, aLen - bLen);        }        else if (bLen > aLen)        {            a = addZeros(a, bLen - aLen);        }             // Updated length        int len = Math.max(aLen, bLen);             // To store the resultant XOR        String res = "";                 for (int i = 0; i < len; i++)        {            if (a.charAt(i) == b.charAt(i))                res += "0";            else                res += "1";        }        return res;    }         // Driver code    public static void main (String[] args)    {        String a = "11001", b = "111111";             System.out.println(getXOR(a, b));    }} // This code is contributed by AnkitRai01
# Python3 implementation of the approach # Function to insert n 0s in the# beginning of the given stringdef addZeros(strr, n):    for i in range(n):        strr = "0" + strr    return strr # Function to return the XOR# of the given stringsdef getXOR(a, b):     # Lengths of the given strings    aLen = len(a)    bLen = len(b)     # Make both the strings of equal lengths    # by inserting 0s in the beginning    if (aLen > bLen):        b = addZeros(b, aLen - bLen)    elif (bLen > aLen):        a = addZeros(a, bLen - aLen)     # Updated length    lenn = max(aLen, bLen);     # To store the resultant XOR    res = ""    for i in range(lenn):        if (a[i] == b[i]):            res += "0"        else:            res += "1"     return res # Driver codea = "11001"b = "111111" print(getXOR(a, b)) # This code is contributed by Mohit Kumar
// C# implementation of the approachusing System; class GFG{         // Function to insert n 0s in the    // beginning of the given string    static String addZeros(String str, int n)    {        for (int i = 0; i < n; i++)        {            str = "0" + str;        }        return str;    }         // Function to return the XOR    // of the given strings    static String getXOR(String a, String b)    {             // Lengths of the given strings        int aLen = a.Length;        int bLen = b.Length;             // Make both the strings of equal lengths        // by inserting 0s in the beginning        if (aLen > bLen)        {            a = addZeros(b, aLen - bLen);        }        else if (bLen > aLen)        {            a = addZeros(a, bLen - aLen);        }             // Updated length        int len = Math.Max(aLen, bLen);             // To store the resultant XOR        String res = "";                 for (int i = 0; i < len; i++)        {            if (a[i] == b[i])                res += "0";            else                res += "1";        }        return res;    }         // Driver code    public static void Main(String[] args)    {        String a = "11001", b = "111111";             Console.WriteLine(getXOR(a, b));    }} // This code is contributed by Rajput-Ji
<script>    // Javascript implementation of the approach         // Function to insert n 0s in the    // beginning of the given string    function addZeros(str, n)    {        for (let i = 0; i < n; i++)        {            str = "0" + str;        }        return str;    }          // Function to return the XOR    // of the given strings    function getXOR(a, b)    {              // Lengths of the given strings        let aLen = a.length;        let bLen = b.length;              // Make both the strings of equal lengths        // by inserting 0s in the beginning        if (aLen > bLen)        {            a = addZeros(b, aLen - bLen);        }        else if (bLen > aLen)        {            a = addZeros(a, bLen - aLen);        }              // Updated length        let len = Math.max(aLen, bLen);              // To store the resultant XOR        let res = "";                  for (let i = 0; i < len; i++)        {            if (a[i] == b[i])                res += "0";            else                res += "1";        }        return res;    }         let a = "11001", b = "111111";            document.write(getXOR(a, b));         // This code is contributed by divyeshrabadiya07.</script>
100110
 
Time Complexity: O(len), len=Max(length a,length b)
Auxiliary Space: O(len)
mohit kumar 29
ankthon
Rajput-Ji
aditya7409
divyeshrabadiya07
ruhelaa48
binary-string
Bitwise-XOR
Competitive Programming
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.