You are viewing a single comment's thread. Return to all comments →
I have a question, why did you have
int significantBit = 31 - Integer.numberOfLeadingZeros(xored);
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
int result = (1 << significantBit) - 1;
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.