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++
#include <bits/stdc++.h>
using namespace std;
void addZeros(string& str, int n)
{
for ( int i = 0; i < n; i++) {
str = "0" + str;
}
}
string getXOR(string a, string b)
{
int aLen = a.length();
int bLen = b.length();
if (aLen > bLen) {
addZeros(b, aLen - bLen);
}
else if (bLen > aLen) {
addZeros(a, bLen - aLen);
}
int len = max(aLen, bLen);
string res = "" ;
for ( int i = 0; i < len; i++) {
if (a[i] == b[i])
res += "0" ;
else
res += "1" ;
}
return res;
}
int main()
{
string a = "11001" , b = "111111" ;
cout << getXOR(a, b);
return 0;
}
|
Java
class GFG
{
static String addZeros(String str, int n)
{
for ( int i = 0 ; i < n; i++)
{
str = "0" + str;
}
return str;
}
static String getXOR(String a, String b)
{
int aLen = a.length();
int bLen = b.length();
if (aLen > bLen)
{
a = addZeros(b, aLen - bLen);
}
else if (bLen > aLen)
{
a = addZeros(a, bLen - aLen);
}
int len = Math.max(aLen, bLen);
String res = "" ;
for ( int i = 0 ; i < len; i++)
{
if (a.charAt(i) == b.charAt(i))
res += "0" ;
else
res += "1" ;
}
return res;
}
public static void main (String[] args)
{
String a = "11001" , b = "111111" ;
System.out.println(getXOR(a, b));
}
}
|
Python3
def addZeros(strr, n):
for i in range (n):
strr = "0" + strr
return strr
def getXOR(a, b):
aLen = len (a)
bLen = len (b)
if (aLen > bLen):
b = addZeros(b, aLen - bLen)
elif (bLen > aLen):
a = addZeros(a, bLen - aLen)
lenn = max (aLen, bLen)
res = ""
for i in range (lenn):
if (a[i] = = b[i]):
res + = "0"
else :
res + = "1"
return res
a = "11001"
b = "111111"
print (getXOR(a, b))
|
C#
using System;
class GFG
{
static String addZeros(String str, int n)
{
for ( int i = 0; i < n; i++)
{
str = "0" + str;
}
return str;
}
static String getXOR(String a, String b)
{
int aLen = a.Length;
int bLen = b.Length;
if (aLen > bLen)
{
a = addZeros(b, aLen - bLen);
}
else if (bLen > aLen)
{
a = addZeros(a, bLen - aLen);
}
int len = Math.Max(aLen, bLen);
String res = "" ;
for ( int i = 0; i < len; i++)
{
if (a[i] == b[i])
res += "0" ;
else
res += "1" ;
}
return res;
}
public static void Main(String[] args)
{
String a = "11001" , b = "111111" ;
Console.WriteLine(getXOR(a, b));
}
}
|
Javascript
<script>
function addZeros(str, n)
{
for (let i = 0; i < n; i++)
{
str = "0" + str;
}
return str;
}
function getXOR(a, b)
{
let aLen = a.length;
let bLen = b.length;
if (aLen > bLen)
{
a = addZeros(b, aLen - bLen);
}
else if (bLen > aLen)
{
a = addZeros(a, bLen - aLen);
}
let len = Math.max(aLen, bLen);
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));
</script>
|
Time Complexity: O(len), len=Max(length a,length b)
Auxiliary Space: O(len)
Approach:
In this approach, we first determine the length of the larger string and iterate over it. For each index, we extract the corresponding bits from both input strings and perform a bitwise XOR operation on them. We then append the result to the output string. Finally, we return the output string.
Note that we use the expression (i < n) ? a[n – i – 1] – ‘0’ : 0 to extract the bit at index i from the string a. This is because the bits in a are stored in reverse order compared to their position in the binary number. So we need to access the bits in reverse order by subtracting i from the length of the string and subtracting 1 (since the index starts from 0). We also subtract ‘0’ from the bit to convert it from a character to an integer. Similarly, we extract the corresponding bit from b and perform the XOR operation.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string getXOR(string a, string b)
{
string result = "" ;
int n = a.length();
int m = b.length();
int len = max(n, m);
for ( int i = 0; i < len; i++) {
int x = (i < n) ? a[n - i - 1] - '0' : 0;
int y = (i < m) ? b[m - i - 1] - '0' : 0;
int z = x ^ y;
result = ( char )(z + '0' ) + result;
}
return result;
}
int main()
{
string a = "11001" , b = "111111" ;
cout << getXOR(a, b);
return 0;
}
|
Java
public class GFG {
public static String getXOR(String a, String b) {
String result = "" ;
int n = a.length();
int m = b.length();
int len = Math.max(n, m);
for ( int i = 0 ; i < len; i++) {
int x = (i < n) ? a.charAt(n - i - 1 ) - '0' : 0 ;
int y = (i < m) ? b.charAt(m - i - 1 ) - '0' : 0 ;
int z = x ^ y;
result = ( char ) (z + '0' ) + result;
}
return result;
}
public static void main(String[] args) {
String a = "11001" ;
String b = "111111" ;
System.out.println(getXOR(a, b));
}
}
|
Python3
def get_xor(a, b):
result = ""
n = len (a)
m = len (b)
length = max (n, m)
for i in range (length):
x = int (a[n - i - 1 ]) if i < n else 0
y = int (b[m - i - 1 ]) if i < m else 0
z = x ^ y
result = str (z) + result
return result
def main():
a = "11001"
b = "111111"
print (get_xor(a, b))
if __name__ = = "__main__" :
main()
|
C#
using System;
class GFG {
static string GetXOR( string a, string b)
{
string result = "" ;
int n = a.Length;
int m = b.Length;
int len = Math.Max(n, m);
for ( int i = 0; i < len; i++) {
int x = (i < n) ? a[n - i - 1] - '0' : 0;
int y = (i < m) ? b[m - i - 1] - '0' : 0;
int z = x ^ y;
result = ( char )(z + '0' ) + result;
}
return result;
}
static void Main()
{
string a = "11001" ;
string b = "111111" ;
Console.WriteLine(GetXOR(a, b));
}
}
|
Javascript
function getXOR(a, b) {
let result = "" ;
const n = a.length;
const m = b.length;
const len = Math.max(n, m);
for (let i = 0; i < len; i++) {
const x = i < n ? parseInt(a[n - i - 1]) : 0;
const y = i < m ? parseInt(b[m - i - 1]) : 0;
const z = x ^ y;
result = z.toString() + result;
}
return result;
}
const a = "11001" ;
const b = "111111" ;
console.log(getXOR(a, b));
|
Time Complexity: O(max(n, m)), where n and m are the lengths of the input strings A and B, respectively.
Space Complexity: O(max(n, m)), as we need to create a new string to store the XOR of the input strings.
Last Updated :
29 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...