Problem

Given an integer x and a zero-based index n, toggle the n-th bit of x and return the result. Toggling flips the bit (0 -> 1 and 1 -> 0) without affecting other bits.

Examples

Example 1

1
2
3
Input: x = 0b01110101, n = 5
Output: 0b01010101
Explanation: the bit at position 5 (counting from 0 at LSB) is flipped from 1 to 0.

Example 2

1
2
3
Input: x = 0b01010101, n = 5
Output: 0b01110101
Explanation: bit at position 5 flipped from 0 to 1.

Solution

Intuition

XORing a bit with 1 flips it and XORing with 0 leaves it unchanged. Construct a mask with a 1 only at n and XOR it with x to toggle the desired bit in one operation.

Approach

  • Build mask = 1 << n.
  • Return x ^ mask.
  • Validate n when necessary (ensure n >= 0 and within machine word size) to avoid undefined shifts in some languages.

Code

1
2
3
4
5
6
7
class Solution {
 public:
    unsigned int toggleNthBit(unsigned int x, unsigned int n) {
        unsigned int mask = 1u << n;
        return x ^ mask;
    }
};
1
2
3
4
5
6
package main

func toggleNthBit(x uint32, n uint) uint32 {
        mask := uint32(1) << n
        return x ^ mask
}
1
2
3
4
5
6
class Solution {
    public int toggleNthBit(int x, int n) {
        int mask = 1 << n;
        return x ^ mask;
    }
}
1
2
3
4
class Solution:
        def toggle_nth_bit(self, x: int, n: int) -> int:
                mask = 1 << n
                return x ^ mask

Complexity

  • ⏰ Time complexity: O(1), one shift and one XOR.
  • 🧺 Space complexity: O(1).

Method 2 — Explicit set/clear (alternate)

Intuition

Read the n-th bit and either clear it (if 1) or set it (if 0). This is explicit but uses the same primitive masks.

Approach

  • Compute b = (x >> n) & 1.
  • If b == 1 clear: x & ~(1 << n).
  • Else set: x | (1 << n).

Code

1
2
3
4
5
6
7
8
class Solution {
 public:
    unsigned int toggleNthBitExplicit(unsigned int x, unsigned int n) {
        unsigned int b = (x >> n) & 1u;
        if (b == 1u) return x & ~(1u << n);
        return x | (1u << n);
    }
};
1
2
3
4
5
6
7
8
9
package main

func toggleNthBitExplicit(x uint32, n uint) uint32 {
        b := (x >> n) & 1
        if b == 1 {
                return x & ^(uint32(1) << n)
        }
        return x | (uint32(1) << n)
}
1
2
3
4
5
6
7
class Solution {
    public int toggleNthBitExplicit(int x, int n) {
        int b = (x >> n) & 1;
        if (b == 1) return x & ~(1 << n);
        return x | (1 << n);
    }
}
1
2
3
4
5
6
class Solution:
        def toggle_nth_bit_explicit(self, x: int, n: int) -> int:
                b = (x >> n) & 1
                if b == 1:
                        return x & ~(1 << n)
                return x | (1 << n)

Complexity

  • ⏰ Time complexity: O(1).
  • 🧺 Space complexity: O(1).

Notes

  • Toggling the n-th bit via XOR is a standard bit hack; XORing twice with the same mask restores the original value — a property used in parity calculations and simple obfuscation.
  • This is a primitive operation rather than a standalone LeetCode problem.