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 += ( 1 << $ i ) * ( $ k * ( $ k - 1 ) / 2 ) ; } return $ ans ; }