Sum of bitwise AND of all possible subsets of given set | C ++ program to calculate sum of Bit - wise and sum of all subsets of an array ; assuming representation of each element is in 32 bit ; iterating array element ; Counting the set bit of array in ith position ; counting subset which produce sum when particular bit position is set . ; multiplying every position subset with 2 ^ i to count the sum . ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define BITS  32 NEW_LINE int andSum ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < BITS ; i ++ ) { int countSetBits = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] & ( 1 << i ) ) countSetBits ++ ; } int subset = ( 1 << countSetBits ) - 1 ; subset = ( subset * ( 1 << i ) ) ; ans += subset ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << andSum ( arr , size ) ; return 0 ; }