Find a number M < N such that difference between their XOR and AND is maximum | Function to flip all bits of N ; Finding most significant bit of N ; Calculating required number ; Return the answer ; Given number ; Function call
function findM ( N ) { var M = 0 ; var MSB = parseInt ( Math . log ( N ) ) ; for ( i = 0 ; i < MSB ; i ++ ) { if ( ( N & ( 1 << i ) ) == 0 ) M += ( 1 << i ) ; } return M ; } var N = 6 ; document . write ( findM ( N ) ) ;