
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sum of Bitwise AND of All Possible Subsets of Given Set
What do you understand by the problem `Sum of bitwise AND of all possible subsets of given set`? Let's decode.
The problem is asking to find the sum of the bitwise AND of all possible subsets of a given set.
Let's try to understand the problem with an example. Suppose we have a set {1, 2, 3}. What are the possible subsets for this set?
The possible subsets are {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, and {1,2,3}. Now lets calculate the bitwise AND for each subset.
The bitwise AND of these subsets can be calculated as follows:
Subset |
Bitwise AND |
Comments |
---|---|---|
{1} |
1 |
|
{2} |
2 |
|
{3} |
3 |
|
{1,2} |
0 |
binary representation of 1 is 001, and that of 2 is 010, so AND of both is 000 |
{1,3} |
1 |
binary representation of 1 is 001, and that of 3 is 011, so AND of both is 001 |
{2,3} |
2 |
binary representation of 2 is 010, and that of 3 is 011, so AND of both is 010 |
{1,2,3} |
00 |
binary representation of 1 is 001, that of 2 is 010, and that of 3 is 011, so AND of all is 000 |
The sum of these bitwise ANDs is 1+2+3+0+1+2+0 = 9. This value 9 is the sum of bitwise AND of all possible subsets for the set {1,2,3}.
Approach
Now, we have understood what the question is asking us to do. Let's have a look at the approach we will use to solve this problem.
Take the size of the set and set elements as user input
Run a loop that iterates over the set until the maximum value in the set
For each iteration run another loop, which iterates over all the elements of the set to calculate the number of integers with set bits and the 1st position, then 2nd position, etc.
Within this loop we also calculate the number of possible subsets using the formula subsets = (1LL << numbersWithBitSet) ? 1
Now end the loop, for each iteration of the inner loop calculate the sum in a variable using the formula total += bit * subsets , where `subsets` is the number of possible subsets , and bits is the iteration used in outer loop which increases by bit = bit <<1
Still, sounds confusing? Let's understand it with an example
Say we have a set ={1,2,3} = {001,010,011}
Now we create a loop that iterates over all the set elements.
-
For iteration 1, bit =1 = 001
Now we have an inner loop to count the number of elements with set first bits.
-
To calculate this use bit & i (here i is the elements of the set )
001 & 001 = 1
001 & 010 = 0
001 & 011= 1
Therefore we get 2 numbers with first bit set to 1.
Total number of subsets which can be formed using the first bit set = 1 << 2 ? 1= 3, where 2 is the count of numbers with first bit set to 1
We can store the sum of all bitwise and in a variable total = total + bits * subsets (010 * 3 = 6)
Similarly for second iteration we will have value 3 and third iteration won't happen as the bit value exceeds maximum value in the set.
Total = 6+3 =9
C++ code implementation
Too much theory? Now, let's get straight to the code.
Here is the C++ code implementation to calculate the sum of bitwise and of all possible subsets of given set.
Example
#include <iostream> #include <vector> using namespace std; void print_and_Subsets(const vector<int>& set) { long long total = 0; // Use "long long" instead of "long" for 64-bit integers for (int bit = 1; bit != 0; bit <<= 1) { int numbersWithBitSet = 0; for (int i : set) { if ((i & bit) != 0) numbersWithBitSet++; } long long subsets = (1LL << numbersWithBitSet) - 1; // Use "1LL" instead of "1L" for 64-bit integers total += bit * subsets; } cout << "Result: " << total << endl; } int main() { int n = 5; vector<int> set = {1, 2, 3, 4, 5}; print_and_Subsets(set); return 0; }
Output
Result: 25
Space Complexity: O(N)
Time Complexity: O(N * log M)
Conclusion
In this article, we have covered how to calculate the sum of bitwise AND of all possible subsets of a given set using bit masking. Hope you were able to grasp the concept in a better way. Keep learning!