find the xor of n-n range

int l=9;

int r=12;



 int x=(l | (l+1)) & (~l | ~(l+1)); 
for(int i=(l+2);i<=r;i++)
{
    x=(x | i) & (~x | ~i); 
}

the value x returns the xor of range(9,12) but i don’t understand this logic. please help.

It’s just simple XOR property. Take two numbers x and y. XOR$(x, y) = (x | y) & ($~x | ~y). In this case you are calculating XOR of 9, 10 first then whatever value comes, you take it’s xor with 11 and so on, upto you reach 12.
Here (9|10)&(~9|~10) = 3. Then (3|11)&(~3|~11) = 8. And finally (8|12)&(~8|~12) = 4. Also 9^10^11^12 = 4.