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.

L ^ r - gets you the maximum XOR value between l and r.

format( a_number, 'b') - returns the binary string of a_number

wrapping that in a len() gets you the length of that binary string which is what you want the length of all 1s to be.

<< - is a bitshift operator, for example x << y shifts the binary of x to the left y times. ie. 2('10') << 1 = 4('100'). 4('100') << 1 = 8('1000')

put it all together and you have 1 left-bitshifted by the length of the number that l^r gives minus 1.
you subtract by 1 for two reasons:
A) when you shift 1 by that number, you have one extra bit in your bitstring
for example, if you 1<<3 you get 8, which is 1000, but the len() is 3, so you want 3 bits of all 1s which brings us to B)
B)when you subtract 8(1000) by 1 you get 7(111) which is the len() of all 1s

## Maximizing XOR

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

Cool! How did you come up with this solution?

L ^ r - gets you the maximum XOR value between l and r.

format( a_number, 'b') - returns the binary string of a_number

wrapping that in a len() gets you the length of that binary string which is what you want the length of all 1s to be.

<< - is a bitshift operator, for example x << y shifts the binary of x to the left y times. ie. 2('10') << 1 = 4('100'). 4('100') << 1 = 8('1000')

put it all together and you have 1 left-bitshifted by the length of the number that l^r gives minus 1.

you subtract by 1 for two reasons: A) when you shift 1 by that number, you have one extra bit in your bitstring for example, if you 1<<3 you get 8, which is 1000, but the len() is 3, so you want 3 bits of all 1s which brings us to B) B)when you subtract 8(1000) by 1 you get 7(111) which is the len() of all 1s

hopefully that makes sense.