Bitwise XOR of a Binary array
Last Updated :
18 Apr, 2023
Given a binary array arr[], the task is to calculate the bitwise XOR of all the elements in this array and print it.
Examples:
Input: arr[] = {“100”, “1001”, “0011”}
Output: 1110
0100 XOR 1001 XOR 0011 = 1110
Input: arr[] = {“10”, “11”, “1000001”}
Output: 1000000
Approach:
- Step 1: First find the maximum-sized binary string.
- Step 2: Make all the binary strings in an array to the size of the maximum sized string, by adding 0s at the Most Significant Bit
- Step 3: Now find the resultant string by performing bitwise XOR on all the binary strings in the array.
For Examples:
- Let the binary array be {“100”, “001”, and “1111”}.
- Here the maximum sized binary string is 4.
- Make all the binary strings in the array of size 4, by adding 0s at the MSB. Now the binary array becomes {“0100”, “0001” and “1111”}
- Performing bitwise XOR on all the binary strings in the array
“0100” XOR “0001” XOR “1111” = “1110”
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the bitwise XOR
// of all the binary strings
void strBitwiseXOR(string* arr, int n)
{
string result;
int max_len = INT_MIN;
// Get max size and reverse each string
// Since we have to perform XOR operation
// on bits from right to left
// Reversing the string will make it easier
// to perform operation from left to right
for (int i = 0; i < n; i++) {
max_len = max(max_len,
(int)arr[i].size());
reverse(arr[i].begin(),
arr[i].end());
}
for (int i = 0; i < n; i++) {
// Add 0s to the end
// of strings if needed
string s;
for (int j = 0;
j < max_len - arr[i].size();
j++)
s += '0';
arr[i] = arr[i] + s;
}
// Perform XOR operation on each bit
for (int i = 0; i < max_len; i++) {
int pres_bit = 0;
for (int j = 0; j < n; j++)
pres_bit = pres_bit ^ (arr[j][i] - '0');
result += (pres_bit + '0');
}
// Reverse the resultant string
// to get the final string
reverse(result.begin(), result.end());
// Return the final string
cout << result;
}
// Driver code
int main()
{
string arr[] = { "1000", "10001", "0011" };
int n = sizeof(arr) / sizeof(arr[0]);
strBitwiseXOR(arr, n);
return 0;
}
Java
// Java implementation of the approach
import java.io.*;
import java.util.*;
class GFG{
// Function to return the
// reverse string
static String reverse(String str)
{
String rev = "";
for(int i = str.length() - 1; i >= 0; i--)
rev = rev + str.charAt(i);
return rev;
}
// Function to return the bitwise XOR
// of all the binary strings
static String strBitwiseXOR(String[] arr, int n)
{
String result = "";
int max_len = Integer.MIN_VALUE;
// Get max size and reverse each string
// Since we have to perform XOR operation
// on bits from right to left
// Reversing the string will make it easier
// to perform operation from left to right
for(int i = 0; i < n; i++)
{
max_len = Math.max(max_len,
(int)arr[i].length());
arr[i] = reverse(arr[i]);
}
for(int i = 0; i < n; i++)
{
// Add 0s to the end
// of strings if needed
String s = "";
for(int j = 0;
j < max_len - arr[i].length();
j++)
s += '0';
arr[i] = arr[i] + s;
}
// Perform XOR operation on each bit
for(int i = 0; i < max_len; i++)
{
int pres_bit = 0;
for(int j = 0; j < n; j++)
pres_bit = pres_bit ^
(arr[j].charAt(i) - '0');
result += (char)(pres_bit + '0');
}
// Reverse the resultant string
// to get the final string
result = reverse(result);
// Return the final string
return result;
}
// Driver code
public static void main(String[] args)
{
String[] arr = { "1000", "10001", "0011" };
int n = arr.length;
System.out.print(strBitwiseXOR(arr, n));
}
}
// This code is contributed by akhilsaini
Python3
# Function to return the bitwise XOR
# of all the binary strings
import sys
def strBitwiseXOR(arr, n):
result = ""
max_len = -1
# Get max size and reverse each string
# Since we have to perform XOR operation
# on bits from right to left
# Reversing the string will make it easier
# to perform operation from left to right
for i in range(n):
max_len = max(max_len, len(arr[i]))
arr[i] = arr[i][::-1]
for i in range(n):
# Add 0s to the end
# of strings if needed
s = ""
# t = max_len - len(arr[i])
for j in range(max_len - len(arr[i])):
s += "0"
arr[i] = arr[i] + s
# Perform XOR operation on each bit
for i in range(max_len):
pres_bit = 0
for j in range(n):
pres_bit = pres_bit ^ (ord(arr[j][i]) - ord('0'))
result += chr((pres_bit) + ord('0'))
# Reverse the resultant string
# to get the final string
result = result[::-1]
# Return the final string
print(result)
# Driver code
if(__name__ == "__main__"):
arr = ["1000", "10001", "0011"]
n = len(arr)
strBitwiseXOR(arr, n)
# This code is contributed by skylags
C#
// C# implementation of the approach
using System;
class GFG{
// Function to return the
// reverse string
static string reverse(string str)
{
string rev = "";
for(int i = str.Length - 1; i >= 0; i--)
rev = rev + str[i];
return rev;
}
// Function to return the bitwise XOR
// of all the binary strings
static string strBitwiseXOR(string[] arr, int n)
{
string result = "";
int max_len = int.MinValue;
// Get max size and reverse each string
// Since we have to perform XOR operation
// on bits from right to left
// Reversing the string will make it easier
// to perform operation from left to right
for(int i = 0; i < n; i++)
{
max_len = Math.Max(max_len,
(int)arr[i].Length);
arr[i] = reverse(arr[i]);
}
for(int i = 0; i < n; i++)
{
// Add 0s to the end
// of strings if needed
string s = "";
for(int j = 0;
j < max_len - arr[i].Length;
j++)
s += '0';
arr[i] = arr[i] + s;
}
// Perform XOR operation on each bit
for(int i = 0; i < max_len; i++)
{
int pres_bit = 0;
for(int j = 0; j < n; j++)
pres_bit = pres_bit ^
(arr[j][i] - '0');
result += (char)(pres_bit + '0');
}
// Reverse the resultant string
// to get the final string
result = reverse(result);
// Return the final string
return result;
}
// Driver code
public static void Main()
{
string[] arr = { "1000", "10001", "0011" };
int n = arr.Length;
Console.Write(strBitwiseXOR(arr, n));
}
}
// This code is contributed by akhilsaini
JavaScript
<script>
// Javascript implementation of the approach
// Function to return the bitwise XOR
// of all the binary strings
function strBitwiseXOR(arr, n)
{
var result = "";
var max_len = -1000000000;
// Get max size and reverse each string
// Since we have to perform XOR operation
// on bits from right to left
// Reversing the string will make it easier
// to perform operation from left to right
for (var i = 0; i < n; i++) {
max_len = Math.max(max_len,
arr[i].length);
arr[i] = arr[i].split('').reverse().join('');
}
for (var i = 0; i < n; i++) {
// Add 0s to the end
// of strings if needed
var s;
for (var j = 0;
j < max_len - arr[i].length;
j++)
s += '0';
arr[i] = arr[i] + s;
}
// Perform XOR operation on each bit
for (var i = 0; i < max_len; i++) {
var pres_bit = 0;
for (var j = 0; j < n; j++)
pres_bit = pres_bit ^ (arr[j][i] - '0'.charCodeAt(0));
result += (pres_bit + '0'.charCodeAt(0));
}
// Reverse the resultant string
// to get the final string
result = result.split('').reverse().join('');
// Return the final string
document.write( result);
}
// Driver code
var arr = ["1000", "10001", "0011"];
var n = arr.length;
strBitwiseXOR(arr, n);
</script>
Time complexity: O(n*m) where n is the number of strings in the array, and m is the maximum length of any string in the array.
Space Complexity: O(n*m)
Similar Reads
XOR in a range of a binary array Given a binary array arr[] of size N and some queries. Each query represents an index range [l, r]. The task is to find the xor of the elements in the given index range for each query i.e. arr[l] ^ arr[l + 1] ^ ... ^ arr[r]. Examples: Input: arr[] = {1, 0, 1, 1, 0, 1, 1}, q[][] = {{0, 3}, {0, 2}} Ou
7 min read
Modify a binary array to Bitwise AND of all elements as 1 Given an array, a[] consists of only 0 and 1. The task is to check if it is possible to transform the array such that the AND value between every pair of indices is 1. The only operation allowed is to: Take two indices i and j and replace the a[i] and a[j] with a[i] | a[j] where '|' means bitwise OR
4 min read
XOR of Bitwise OR Pairs from Two Arrays Given two integer arrays A[] and B[] of size N and M respectively. Another array C is formed of size N*M, containing Bitwise OR of all possible pairs of elements of A with all elements of B i.e. A[i] | B[j] for all valid i and j. Find the Bitwise XOR of array C i.e. C [0] ^ C[1] ^ .......C[N*M]. Exa
7 min read
Find XOR of all elements in an Array Given an array arr[] containing integers of size N, the task is to find the XOR of this array.Examples: Input: arr[] = {2, 4, 7} Output: 1 Explanation: XOR of the array = 2 ^ 4 ^ 7 = 1Input: arr[] = { 3, 9, 12, 13, 15 } Output: 4 Approach: In order to find the XOR of all elements in the array, we si
5 min read
Bitwise XOR of Bitwise AND of all pairs from two given arrays Given two arrays arr1[] and arr2[] consisting of N and M integers respectively, the task is to print the Bitwise XOR of Bitwise AND of all pairs possible by selecting an element from arr1[] and arr2[]. Examples: Input: arr1[] = {1, 2, 3}, arr2[] = {6, 5}Output: 0Explanation: Bitwise AND of the pair
10 min read