Simple 6 lines c++ solution


#1

int Solution::cntBits(vector &A) {
long long b[32]={},n=A.size(),ans=0,m=1e9+7;
for(int i=0;i<32;i++)
for(int j:A) if(j & (1<<i)) b[i]++;
for(int i=0;i<32;i++)
for(int j:A) if(j & (1<<i)) ans=(ans%m+(n-b[i])%m)%m;
else ans=(ans%m+b[i]%m)%m;
return ans;
}


#2
#define m 1000000007
int Solution::cntBits(vector<int> &A) 
{
    long long int sum = 0, n = A.size();
    for(int i=0, iset=0; i<32; i++)
    {
        for(int j=0; j<n; j++)
            iset += (A[j]>>i)%2;

        sum = (sum + (iset*(n-iset))%m)%m;
    }
    return (sum<<1)%m;
}