Maximum XOR value of a pair from a range | method to get maximum xor value in range [ L , R ] ; get xor of limits ; loop to get msb position of L ^ R ; construct result by adding 1 , msbPos times ; Driver Code
< ? php function maxXORInRange ( $ L , $ R ) { $ LXR = $ L ^ $ R ; $ msbPos = 0 ; while ( $ LXR ) { $ msbPos ++ ; $ LXR >>= 1 ; } $ maxXOR = 0 ; $ two = 1 ; while ( $ msbPos -- ) { $ maxXOR += $ two ; $ two <<= 1 ; } return $ maxXOR ; } $ L = 8 ; $ R = 20 ; echo maxXORInRange ( $ L , $ R ) , " STRNEWLINE " ; ? >