XOR of two Binary Strings
Given two binary strings A and B of equal lengths, the task is to print a string that is the XOR of Binary Strings A and B.
Examples:
Input: A = “0001”, B = “0010”
Output: 0011
Input: A = “1010”, B = “0101”
Output: 1111
Approach: The idea is to iterate over both the string character by character and if the character is 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++
#include<bits/stdc++.h>
using namespace std;
string xoring(string a, string b, int n){
string ans = "" ;
for ( int i = 0; i < n; i++)
{
if (a[i] == b[i])
ans += "0" ;
else
ans += "1" ;
}
return ans;
}
int main()
{
string a = "1010" ;
string b = "1101" ;
int n = a.length();
string c = xoring(a, b, n);
cout << c << endl;
}
|
Java
import java.io.*;
class GFG {
static String xoring(String a, String b, int n){
String ans = "" ;
for ( int i = 0 ; i < n; i++)
{
if (a.charAt(i) == b.charAt(i))
ans += "0" ;
else
ans += "1" ;
}
return ans;
}
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);
}
}
|
Python3
def xor(a, b, n):
ans = ""
for i in range (n):
if (a[i] = = b[i]):
ans + = "0"
else :
ans + = "1"
return ans
if __name__ = = "__main__" :
a = "1010"
b = "1101"
n = len (a)
c = xor(a, b, n)
print (c)
|
C#
using System;
class GFG{
static string xoring( string a, string b, int n){
string ans = "" ;
for ( int i = 0; i < n; i++)
{
if (a[i] == b[i])
ans += "0" ;
else
ans += "1" ;
}
return ans;
}
static public void Main ()
{
string a = "1010" ;
string b = "1101" ;
int n = a.Length;
string c = xoring(a, b, n);
Console.WriteLine(c);
}
}
|
Javascript
<script>
function xoring(a, b, n){
let ans = "" ;
for (let i = 0; i < n; i++)
{
if (a[i] == b[i])
ans += "0" ;
else
ans += "1" ;
}
return ans;
}
let a = "1010" ;
let b = "1101" ;
let n = a.length;
let c = xoring(a, b, n);
document.write(c);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
method2:performs XOR operation bit by bit to generate the XOR result string.
Approach: We take two binary strings A and B as input which are of equal length, and apply for loop and performs XOR operation bit by bit to generate the XOR result. Then we print the XOR result string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
std::string xor_Binary_Strings( string &A,string &B) {
int len = A.length();
string result = "" ;
for ( int i = 0; i < len; i++) {
char currentXOR = ((A[i] - '0' ) ^ (B[i] - '0' )) + '0' ;
result += currentXOR;
}
return result;
}
int main() {
string A= "1010" ;
string B = "1101" ;
int n = A.length();
string C =xor_Binary_Strings(A,B);
cout << C << endl;
return 0;
}
|
Java
public class Main {
public static String xorBinaryStrings(String A,
String B)
{
int len = A.length();
StringBuilder result
= new StringBuilder();
for ( int i = 0 ; i < len; i++) {
char currentXOR
= ( char )((A.charAt(i) - '0' )
^ (B.charAt(i) - '0' ) + '0' );
result.append(currentXOR);
}
return result.toString();
}
public static void main(String[] args)
{
String A = "1010" ;
String B = "1101" ;
int n = A.length();
String C = xorBinaryStrings(A, B);
System.out.println(C);
}
}
|
Python
def xor_binary_strings(A, B):
length = len (A)
result = ""
for i in range (length):
current_xor = str ( int (A[i]) ^ int (B[i]))
result + = current_xor
return result
def main():
A = "1010"
B = "1101"
C = xor_binary_strings(A, B)
print (C)
if __name__ = = "__main__" :
main()
|
C#
using System;
class Program {
static string XorBinaryStrings( string A, string B)
{
int len = A.Length;
string result = "" ;
for ( int i = 0; i < len; i++) {
char currentXOR
= ( char )((A[i] - '0' ) ^ (B[i] - '0' ) + '0' );
result += currentXOR;
}
return result;
}
static void Main( string [] args)
{
string A = "1010" ;
string B = "1101" ;
int n = A.Length;
string C = XorBinaryStrings(A, B);
Console.WriteLine(C);
}
}
|
Javascript
function xorBinaryStrings(A, B) {
const len = A.length;
let result = '' ;
for (let i = 0; i < len; i++) {
const currentXOR = String(Number(A[i]) ^ Number(B[i]));
result += currentXOR;
}
return result;
}
function main() {
const A = '1010' ;
const B = '1101' ;
const C = xorBinaryStrings(A, B);
console.log(C);
}
main();
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
03 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...