Maximizing XOR Discussions | Algorithms | HackerRank
  • [deleted]
    + 3 comments

    You can use bit shifting and bitwise or checks to get your answer a lot quicker, you avoid doing any loops whatsoever.

    (Edit: updated to reflect changes from godoh's comment)

    int value = l ^ r;
    
    value |= value >> 1;
    value |= value >> 2;
    value |= value >> 4;
    value |= value >> 8;
    return value;