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

0100XOR1001XOR0011 = 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 the array of the size of 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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

11010

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:

- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Replace every element of the array with BitWise XOR of all other
- Replace every array element by Bitwise Xor of previous and next element
- Minimum Bitwise XOR operations to make any two array elements equal
- Bitwise XOR of a submatrix of a matrix generated from a given array
- Construct the Array using given bitwise AND, OR and XOR
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Count pairs with Bitwise XOR as ODD number
- Count pairs with Bitwise XOR as EVEN number
- Count subsequences with same values of Bitwise AND, OR and XOR
- Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them
- Queries to update each element in subarray to Bitwise XOR with a given value
- Maximum sum of Bitwise XOR of all elements of two equal length subsets
- Bitwise XOR of elements having odd frequency
- Find XOR of two number without using XOR operator
- Given a set, find XOR of the XOR's of all subsets.
- Choose X such that (A xor X) + (B xor X) is minimized

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.