Count of values of x <= n for which ( n XOR x ) = ( n | Function to return the count of valid values of x ; Convert n into binary String ; To store the count of 1 s ; If current bit is 1 ; Calculating answer ; Driver code
function countX ( n ) { let binary = ( n >>> 0 ) . toString ( 2 ) ; let count = 0 ; for ( let i = 0 ; i < binary . length ; i ++ ) { if ( binary [ i ] == ' ' ) count ++ ; } let answer = Math . floor ( Math . pow ( 2 , count ) ) ; return answer ; } let n = 5 ; let answer = countX ( n ) ; document . write ( answer ) ;