Find smallest number n such that n XOR n + 1 equals to given k . | function to return the required n ; if k is of form 2 ^ i - 1 ; driver program
function xorCalc ( k ) { if ( k == 1 ) return 2 ; if ( ( ( k + 1 ) & k ) == 0 ) return parseInt ( k / 2 ) ; return 1 ; } var k = 31 ; document . write ( xorCalc ( k ) ) ;