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++
// 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 Strings 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 int 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
// Java Implementation to find the // XOR of the two Binary Strings import 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 |
Python3
# Python Implementation to find the # XOR of the two Binary Strings # Function to find the # XOR of the two Binary Strings def 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 Code if __name__ == "__main__": a = "1010" b = "1101" n = len(a) c = xor(a, b, n) print(c) |
C#
// C# Implementation to find the // XOR of the two Binary Strings using 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 |
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:
- XOR two binary strings of unequal lengths
- Find XOR of two number without using XOR operator
- Rearrange given binary strings to maximize their Bitwise XOR value
- Given a set, find XOR of the XOR's of all subsets.
- Choose X such that (A xor X) + (B xor X) is minimized
- Minimum flips in two binary arrays so that their XOR is equal to another array
- XOR of two numbers after making length of their binary representations equal
- XOR of path between any two nodes in a Binary Tree
- 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 binary strings
- Given two binary strings perform operation until B > 0 and print the result
- Count of distinct XORs formed by rearranging two Binary strings
- Ways to remove one element from a binary string so that XOR becomes zero
- Count of cyclic permutations having XOR with other binary string as 0
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
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.


