We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

Your version of the code works also. I used 31 instead of 32 since I count the 32 bits from 0 to 31 instead of 1 to 32. That is, I count the leftmost bit as the 31st bit, and the rightmost bit as the 0th bit.

## Maximizing XOR

You are viewing a single comment's thread. Return to all comments →

I have a question, why did you have

Instead of 32, which represents the 32bits used for an int.

Using the base example of L = 10, R = 15

L ^ R = 1010 ^ 1111 = 0101 = 5

The result of

Integer.numberOfLeadingZeros(xored)is 29, which matches 32bits - 3bits (for 5)And then your result would be

Just trying to learn on your reasoning to use 31.

Your version of the code works also. I used 31 instead of 32 since I count the 32 bits from 0 to 31 instead of 1 to 32. That is, I count the leftmost bit as the 31st bit, and the rightmost bit as the 0th bit.

HackerRank solutions.