Maximum Xor Problem

Please explain the Editorial i am not understanding it.

An efficient solution is to consider pattern of binary values from L to R. We can see that first bit from L to R either changes from 0 to 1 or it stays 1 i.e. if we take the XOR of any two numbers for maximum value their first bit will be fixed which will be same as first bit of XOR of L and R itself. After observing the technique to get first bit, we can see that if we XOR L and R, the most significant bit of this XOR will tell us the maximum value we can achieve i.e. let XOR of L and R is 1xxx where x can be 0 or 1 then maximum XOR value we can get is 1111 because from L to R we have all possible combination of xxx and it is always possible to choose these bits in such a way from two numbers such that their XOR becomes all 1.
Here is the code https://ide.codingblocks.com/s/239341

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

I was not able to understand this problem so i came to this page…can u elaborate more because i want to know the reason that why are u subtracting 1 from the result??

@keshavgupta0103 please do respnd

for example if the maximum xor is 7, then the result formed by taking powers of 2 would be 8. so answer would be 8-1 = 7.

1 Like

thanks…i got it now .

when we get the msb why not we calculate (2^msb) -1.