The question is to find XOR of the XOR’s of all subsets. i.e if the set is {1,2,3}. All subsets are : [{1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}]. Find the XOR of each of the subset and then find the XOR of every subset result.

**We strongly recommend you to minimize your browser and try this yourself first.**

This is a very simple question to solve if we get the first step (and only step) right. The solution is XOR is always 0 when n > 1 and Set[0] when n is 1.

## C++

`// C++ program to find XOR of XOR's of all subsets ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns XOR of all XOR's of given subset ` `int` `findXOR(` `int` `Set[], ` `int` `n) ` `{ ` ` ` `// XOR is 1 only when n is 1, else 0 ` ` ` `if` `(n == 1) ` ` ` `return` `Set[0]; ` ` ` `else` ` ` `return` `0; ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `Set[] = {1, 2, 3}; ` ` ` `int` `n = ` `sizeof` `(Set)/` `sizeof` `(Set[0]); ` ` ` `cout << ` `"XOR of XOR's of all subsets is "` ` ` `<< findXOR(Set, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find XOR of ` `// XOR's of all subsets ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` `// Returns XOR of all XOR's of given subset ` `static` `int` `findXOR(` `int` `Set[], ` `int` `n) { ` ` ` ` ` `// XOR is 1 only when n is 1, else 0 ` ` ` `if` `(n == ` `1` `) ` ` ` `return` `Set[` `0` `]; ` ` ` `else` ` ` `return` `0` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String arg[]) ` `{ ` ` ` `int` `Set[] = {` `1` `, ` `2` `, ` `3` `}; ` ` ` `int` `n = Set.length; ` ` ` `System.out.print(` `"XOR of XOR's of all subsets is "` `+ ` ` ` `findXOR(Set, n)); ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find ` `# XOR of XOR's of all subsets ` ` ` `# Returns XOR of all ` `# XOR's of given subset ` `def` `findXOR(` `Set` `, n): ` ` ` ` ` `# XOR is 1 only when ` ` ` `# n is 1, else 0 ` ` ` `if` `(n ` `=` `=` `1` `): ` ` ` `return` `Set` `[` `0` `] ` ` ` `else` `: ` ` ` `return` `0` ` ` `# Driver code ` ` ` `Set` `=` `[` `1` `, ` `2` `, ` `3` `] ` `n ` `=` `len` `(` `Set` `) ` ` ` `print` `(` `"XOR of XOR's of all subsets is "` `, ` ` ` `findXOR(` `Set` `, n)); ` ` ` `# This code is contributed ` `# by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find XOR of ` `// XOR's of all subsets ` `using` `System; ` ` ` `class` `GFG { ` ` ` `// Returns XOR of all ` `// XOR's of given subset ` `static` `int` `findXOR(` `int` `[]Set, ` `int` `n) ` `{ ` ` ` ` ` `// XOR is 1 only when n ` ` ` `// is 1, else 0 ` ` ` `if` `(n == 1) ` ` ` `return` `Set[0]; ` ` ` `else` ` ` `return` `0; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `[]Set = {1, 2, 3}; ` ` ` `int` `n = Set.Length; ` ` ` `Console.Write(` `"XOR of XOR's of all subsets is "` `+ ` ` ` `findXOR(Set, n)); ` `} ` `} ` ` ` `// This code is contributed by nitin mittal ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find XOR ` `// of XOR's of all subsets ` ` ` `// Returns XOR of all ` `// XOR's of given subset ` `function` `findXOR(` `$Set` `, ` `$n` `) ` `{ ` ` ` ` ` `// XOR is 1 only when ` ` ` `// n is 1, else 0 ` ` ` `if` `(` `$n` `== 1) ` ` ` `return` `$Set` `[0]; ` ` ` `else` ` ` `return` `0; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$Set` `= ` `array` `(1, 2, 3); ` ` ` `$n` `= ` `count` `(` `$Set` `); ` ` ` `echo` `"XOR of XOR's of all subsets is "` ` ` `, findXOR(` `$Set` `, ` `$n` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

Output:

XOR of XOR's of all subsets is 0

**Related Problem : **

Sum of XOR of all possible subsets

**How does this work?**

The logic goes simple. Let us consider n’th element, it can be included in all subsets of remaining (n-1) elements. The number of subsets for (n-1) elements is equal to 2^{(n-1)} which is always even when n>1. Thus, in the XOR result, every element is included even number of times and XOR of even occurrences of any number is 0.

This article is contributed by **Ekta Goel**. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

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:

- Sum of XOR of all possible subsets
- Maximum sum of Bitwise XOR of all elements of two equal length subsets
- Count number of subsets having a particular XOR value
- Count no. of ordered subsets having a particular XOR value
- Number of subsets with same AND, OR and XOR values in an Array
- Sum of bitwise OR of all possible subsets of given set
- Sum of bitwise AND of all possible subsets of given set
- Find XOR of two number without using XOR operator
- Find the XOR of the elements in the given range [L, R] with the value K for a given set of queries
- Choose X such that (A xor X) + (B xor X) is minimized
- Find the maximum subset XOR of a given set
- Printing all subsets of {1,2,3,...n} without using array or loop
- Subset array sum by generating all the subsets
- Partition of a set into K subsets with equal sum using BitMask and DP
- XOR of all subarray XORs | Set 2
- XOR of all subarray XORs | Set 1
- XOR of all elements of array with set bits equal to K
- Count set bits in Bitwise XOR of all adjacent elements upto N
- Find all possible triangles with XOR of sides zero
- Find the original Array using XOR values of all adjacent elements