Maximise XOR of a given integer with a number from the given range

Given q queries each of specifies three integers x, l, r. We have to find an integer from given range [l, r] inclusive, such that it gives maximum XOR with x. All values are assumed to be positive. We will show two ways to solve this interesting problem. One is the native approach which take O(R-L) time complexity and the other one is an efficient algorithm that takes only O(log X) time complexity.

This is a companion discussion topic for the original entry at