flipNthBit
Write a function that flips the nth bit of a given integer (0-indexed from the right). For example, flipping the 2nd bit of 10 (binary 1010) results in 14 (binary 1110).
Examples
flipNthBit(10, 2) → 14
flipNthBit(10, 1) → 8
Starter code
function flipNthBit(num, n) {
}
Complexity of the optimal solution
Time O(1), space O(1). This solution uses the bitwise XOR operator. A mask `(1 << n)` is created with only the nth bit set. XORing the number with this mask flips exactly that bit. These are all constant time operations.