XOR Profit Problem

Please explain the efficient approach to solve this problem.

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. It is explained below with some examples,

Examples 1:
L = 8 R = 20
L ^ R = (01000) ^ (10100) = (11100)
Now as L ^ R is of form (1xxxx) we
can get maximum XOR as (11111) by
choosing A and B as 15 and 16 (01111
and 10000)

int maxXORInRange(int L, int R)
{
// get xor of limits
int LXR = L ^ R;

//  loop to get msb position of L^R 
int msbPos = 0; 
while (LXR) 
{ 
    msbPos++; 
    LXR >>= 1; 
} 

// construct result by adding 1, 
// msbPos times 
int maxXOR = 0; 
int two = 1; 
while (msbPos--) 
{ 
    maxXOR += two; 
    two <<= 1; 
} 

return maxXOR; 

}
understand this, this is a very efficient solution