Skip to main content
Back to problems
Leetcode
Medium
Bit Manipulation
Backtracking
Combinatorics
Sum Of All Subset Xor Totals

Compute the sum of XOR values over every subset of an array.

Acceptance 100%
Problem Statement

Problem

Given an integer array nums, consider every possible subset of nums, including the empty subset.

For each subset, compute the bitwise XOR of all its elements. Return the sum of these XOR values across all subsets.

A subset is any selection of elements from the array, preserving no particular order. Different positions in the array are treated as distinct elements.

Notes

  • The XOR of an empty subset is 0.
  • You must count every subset exactly once.
  • The array may contain repeated values, but subsets are formed from indices, not values.

Input Format

  • A single integer array nums.
  • Each element is an integer value used in bitwise XOR operations.

Output Format

  • Return an integer representing the sum of XOR totals over all subsets of nums.

Constraints

  • 0 <= nums.length <= 20
  • 0 <= nums[i] <= $10^{9}$
  • The answer fits in a 64-bit signed integer for the intended constraints.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [1,3]

Output

6

Explanation

Subsets are [], [1], [3], [1,3]. Their XOR totals are 0, 1, 3, 2. Sum = 0 + 1 + 3 + 2 = 6.

Example 2

Input

nums = [5,1,6]

Output

28

Explanation

There are 8 subsets in total. The XOR totals are 0, 5, 1, 6, 4, 3, 7, 2, and their sum is 28.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.