Maximizing XOR Discussions | Algorithms | HackerRank
  • + 1 comment

    Solution in O(g) time, where g is the MSF of (l xor r). For a 32 bit unsigned integer, worst-case g would be 32 operations.

    int maxXor(int l, int r) 
    {
        int a = l ^ r;
        int max = 0;
        while (a != 0)
        {
            max |= a;
            a = a >> 1;
        }
        return max;
    }