XOR two binary strings of unequal lengths

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: 100110



Input: 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to insert n 0s in the
// beginning of the given string
void addZeros(string& str, int n)
{
    for (int i = 0; i < n; i++) {
        str = "0" + str;
    }
}
  
// Function to return the XOR
// of the given strings
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) {
        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 code
int main()
{
    string a = "11001", b = "111111";
  
    cout << getXOR(a, b);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
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.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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to insert n 0s in the
# beginning of the given strring
def addZeros(strr, n):
    for i in range(n):
        strr = "0" + strr
    return strr
  
# Function to return the XOR
# of the given strrings
def getXOR(a, b):
  
    # Lengths of the given strrings
    aLen = len(a)
    bLen = len(b)
  
    # Make both the strrings 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 code
a = "11001"
b = "111111"
  
print(getXOR(a, b))
  
# This code is contributed by Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using 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

chevron_right


Output:

100110


My Personal Notes arrow_drop_up

Learner

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.