Count of pairs whose bitwise AND is a power of 2 | Method to check if x is power of 2 ; First x in the below expression is for the case when x is 0 ; Function to return the number of valid pairs ; Iterate for all possible pairs ; Bitwise and value of the pair is passed ; Return the final count ; Given array arr [ ] ; Function call
function check ( x ) { return x != 0 && ( ( x & ( x - 1 ) ) == 0 ) ; } function count ( arr , n ) { let cnt = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { if ( check ( arr [ i ] & arr [ j ] ) ) cnt ++ ; } } return cnt ; } let arr = [ 6 , 4 , 2 , 3 ] ; let n = arr . length ; document . write ( count ( arr , n ) ) ;