There are k set bits , means k ( k - 1 ) / 2 pairs . Every pair adds 2 ^ i to the answer . Therefore , we add "2 ^ i ▁ * ▁ [ k * ( k - 1 ) / 2 ] " to the answer .
ans = ans + ( 1 << i ) * ( k * ( k - 1 ) // 2 ) NEW_LINE return ans NEW_LINE