# Sum of XOR of all possible subsets

Given an array arr[] of size n, we need to find the sum of all the values that come from XORing all the elements of the subsets.

Input : arr[] = {1, 5, 6} Output : 28 Total Subsets = 2^{3}1 = 1 5 = 5 6 = 6 1 ^ 5 = 4 1 ^ 6 = 7 5 ^ 6 = 3 1 ^ 5 ^ 6 = 2 0(empty subset) Now SUM of all these XORs = 1 + 5 + 6 + 4 + 7 + 3 + 2 + 0 = 28 Input : arr[] = {1, 2} Output : 6

A **Naive approach** is to take the XOR all possible combinations of array[] elements and then perform the summation of all values. **Time complexity** of this approach grows exponentially so it would not be better for a large value of n.

An **Efficient** approach is to find the pattern with respect to the property of XOR. Now again consider the subset in binary form like:

1 = 001 5 = 101 6 = 110 1 ^ 5 = 100 1 ^ 6 = 111 5 ^ 6 = 011 1^5^6 = 010

So if we analyze all these binary numbers of the XORs, we can observe that set bit occurs at all the positions of i(0 to n-1) will exactly contribute to half of 2^{n}. So we can easily impose these two conditions at each such position of i.

- If there is any value of arr[] that has set i
^{th}bit set, then exactly half of 2^{n}subsets will be of the form, so they will contribute to 2^{n-1+i}to the final sum. - If there is no value of arr[] that i
^{th}bit set, then we can say that there will be no term in all subsets that have a i^{th}bit set.

The proof of the above point is as follows:**Case 1:**

Lets assume there are k elements in the array with i^{th} bit set and k is not zero.

So, to have a subset with ith bit set in its xor, we need it to have odd number of elements with i^{th} bit set.

Number of ways to choose elements with i^{th} bit not set = 2^{(n-k)}

Number of ways to choose elements with i^{th} bit set = ^{k}C_{1} + ^{k}C_{3} + ^{k}C_{5} …. = 2^{(k-1)}

Total number of ways = 2^{(n-1)}

Thus, the contribution towards sum becomes, 2^{(n+i-1)}**Case 2:**

If no element has i^{th} bit set, i.e. k = 0, the contribution of i^{th} bit towards total sum remains 0.

Now the question boils down to check which position of element of the arr[] will be set or not. But here is some trick that we will not iterate for all elements one by one in spite of that we can simple take the OR of all such values and multiply with 2^{n-1}, For example:-

Take a OR of all arr[] elements, we get= 1 | 5 | 6 = 001 | 101 | 110 = 111Now to find final summation, we can write it down as:-= 1*2^{n-1+2}+ 1*2^{n-1+1}+ 1*2^{n-1+0}= 2^{n-1}* (1*2^{2}+ 1*2^{1}+ 1*2^{0}) = 2^{n-1}* (111_{2}) = 2^{n-1}* 7Put n = 3, we get= 28

So at last for any value of n and array elements, we can simple say that the final sum will be 2^{n-1} times the bitwise OR of all the inputs.

## C++

`// Below is C++ approach to finding the XOR_SUM` `#include<bits/stdc++.h>` `using` `namespace` `std;` `// Returns sum of XORs of all subsets` `int` `xorSum(` `int` `arr[], ` `int` `n)` `{` ` ` `int` `bits = 0;` ` ` `// Finding bitwise OR of all elements` ` ` `for` `(` `int` `i=0; i < n; ++i)` ` ` `bits |= arr[i];` ` ` `int` `ans = bits * ` `pow` `(2, n-1);` ` ` `return` `ans;` `}` `// Driver code` `int` `main()` `{` ` ` `int` `arr[] = {1, 5, 6};` ` ` `int` `size = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `cout << xorSum(arr, size);` `}` |

## Java

`// Java approach to finding the XOR_SUM` `class` `GFG {` ` ` ` ` `// Returns sum of XORs of all subsets` ` ` `static` `int` `xorSum(` `int` `arr[], ` `int` `n)` ` ` `{` ` ` ` ` `int` `bits = ` `0` `;` ` ` ` ` `// Finding bitwise OR of all elements` ` ` `for` `(` `int` `i = ` `0` `; i < n; ++i)` ` ` `bits |= arr[i];` ` ` ` ` `int` `ans = bits * (` `int` `)Math.pow(` `2` `, n-` `1` `);` ` ` ` ` `return` `ans;` ` ` `}` ` ` ` ` `// Driver method` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` ` ` `int` `arr[] = {` `1` `, ` `5` `, ` `6` `};` ` ` `int` `size = arr.length;` ` ` ` ` `System.out.print(xorSum(arr, size));` ` ` `}` `}` `// This code is contributed by Anant Agarwal.` |

## Python3

`# Python3 approach to finding the XOR_SUM` `# Returns sum of XORs of all subsets` `def` `xorSum(arr, n):` ` ` `bits ` `=` `0` ` ` `# Finding bitwise OR of all elements` ` ` `for` `i ` `in` `range` `(n):` ` ` `bits |` `=` `arr[i]` ` ` `ans ` `=` `bits ` `*` `pow` `(` `2` `, n` `-` `1` `)` ` ` `return` `ans` `# Driver Code` `arr ` `=` `[` `1` `, ` `5` `, ` `6` `]` `size ` `=` `len` `(arr)` `print` `(xorSum(arr, size))` `# This code is contributed by Anant Agarwal.` |

## C#

`// C# approach to finding the XOR_SUM` `using` `System;` `class` `GFG {` ` ` ` ` `// Returns sum of XORs of all subsets` ` ` `static` `int` `xorSum(` `int` `[]arr, ` `int` `n)` ` ` `{` ` ` ` ` `int` `bits = 0;` ` ` ` ` `// Finding bitwise OR of all elements` ` ` `for` `(` `int` `i = 0; i < n; ++i)` ` ` `bits |= arr[i];` ` ` ` ` `int` `ans = bits * (` `int` `)Math.Pow(2, n - 1);` ` ` ` ` `return` `ans;` ` ` `}` ` ` ` ` `// Driver code` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` ` ` `int` `[]arr = {1, 5, 6};` ` ` `int` `size = arr.Length;` ` ` ` ` `Console.Write(xorSum(arr, size));` ` ` `}` `}` `// This code is contributed by Nitin Mittal.` |

## PHP

`<?php` `// PHP program to finding the XOR_SUM` `// Returns sum of XORs of all subsets` `function` `xorSum(` `$arr` `, ` `$n` `)` `{` ` ` `$bits` `= 0;` ` ` `// Finding bitwise OR` ` ` `// of all elements` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ++` `$i` `)` ` ` `$bits` `|= ` `$arr` `[` `$i` `];` ` ` `$ans` `= ` `$bits` `* pow(2, ` `$n` `- 1);` ` ` `return` `$ans` `;` `}` ` ` `// Driver code` ` ` `$arr` `= ` `array` `(1, 5, 6);` ` ` `$size` `= sizeof(` `$arr` `);` ` ` `echo` `xorSum(` `$arr` `, ` `$size` `);` `// This code is contributed by nitin mittal.` `?>` |

## Javascript

`<script>` `// Below is JavaScript approach to finding the XOR_SUM` `// Returns sum of XORs of all subsets` `function` `xorSum(arr, n)` `{` ` ` `let bits = 0;` ` ` `// Finding bitwise OR of all elements` ` ` `for` `(let i=0; i < n; ++i)` ` ` `bits |= arr[i];` ` ` `let ans = bits * Math.pow(2, n-1);` ` ` `return` `ans;` `}` `// Driver code` ` ` `let arr = [1, 5, 6];` ` ` `let size = arr.length;` ` ` `document.write(xorSum(arr, size));` `// This code is contributed by Surbhi Tyagi.` `</script>` |

**Output:**

28

**Time complexity: **O(n) **Auxiliary space: **O(1)**Related Problems: **

Given a set, find XOR of the XORâ€™s of all subsets.

Find sum of sum of all sub-sequences**Reference:**

http://math.stackexchange.com/questions/712487/finding-xor-of-all-subsets?newreg=293ddec5b7614b7fa4c50b4e4d710a4b

This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.