Problem

Given an integer x and two bit indices i and j (0-based, LSB is index 0), swap the bits at positions i and j and return the resulting integer. Perform the swap only when the bits differ; if they are equal the integer remains unchanged.

Examples

Example 1

1
2
3
Input: x = 0b10110, i = 1, j = 3
Output: 0b01110
Explanation: bits at positions 1 and 3 are different and get swapped.

Example 2

1
2
3
Input: x = 0b1001, i = 0, j = 3
Output: 0b1001
Explanation: bits are the same, so x is unchanged.

Solution

Intuition

If the bits at i and j differ, flipping both bits (XOR with a mask that has ones at i and j) effectively swaps them. If they are the same, no change is needed.

Approach

  • Extract bi = (x >> i) & 1 and bj = (x >> j) & 1.
  • If bi != bj, compute mask = (1 << i) | (1 << j) and return x ^ mask.
  • Otherwise return x.

Edge cases:

  • Ensure i and j are within the word width (e.g., 0 <= i,j < 32 for 32-bit). Use unsigned shifts or guards where language requires.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
 public:
  unsigned int swapBits(unsigned int x, unsigned i, unsigned j) {
    unsigned int bi = (x >> i) & 1u;
    unsigned int bj = (x >> j) & 1u;
    if (bi != bj) {
      unsigned int mask = (1u << i) | (1u << j);
      return x ^ mask;
    }
    return x;
  }

};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
package main

func swapBits(x uint32, i, j uint) uint32 {
    bi := (x >> i) & 1
    bj := (x >> j) & 1
    if bi != bj {
        mask := (uint32(1) << i) | (uint32(1) << j)
        return x ^ mask
    }
    return x
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
  public int swapBits(int x, int i, int j) {
    int bi = (x >> i) & 1;
    int bj = (x >> j) & 1;
    if (bi != bj) {
      int mask = (1 << i) | (1 << j);
      return x ^ mask;
    }
    return x;
  }
}
1
2
3
4
5
6
7
8
class Solution:
    def swap_bits(self, x: int, i: int, j: int) -> int:
        bi = (x >> i) & 1
        bj = (x >> j) & 1
        if bi != bj:
            mask = (1 << i) | (1 << j)
            return x ^ mask
        return x

Complexity

  • ⏰ Time complexity: O(1), constant number of bit operations.
  • 🧺 Space complexity: O(1).

Method 2 — Extract and set explicitly (alternate)

Intuition

Read the two bits and build the resulting integer by clearing those positions and writing them back swapped. This is more verbose but explicit.

Approach

  • Extract bi and bj as in Method 1.
  • If bi == bj, return x.
  • Clear positions i and j with x & ~((1<<i)|(1<<j)).
  • Set bit i to bj and bit j to bi and return the result.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
 public:
  unsigned int swapBitsExplicit(unsigned int x, unsigned i, unsigned j) {
    unsigned int bi = (x >> i) & 1u;
    unsigned int bj = (x >> j) & 1u;
    if (bi == bj) return x;
    unsigned int cleared = x & ~((1u << i) | (1u << j));
    unsigned int res = cleared | (bj << i) | (bi << j);
    return res;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package main

func swapBitsExplicit(x uint32, i, j uint) uint32 {
    bi := (x >> i) & 1
    bj := (x >> j) & 1
    if bi == bj {
        return x
    }
    cleared := x & ^((uint32(1) << i) | (uint32(1) << j))
    res := cleared | (bj << i) | (bi << j)
    return res
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
  public int swapBitsExplicit(int x, int i, int j) {
    int bi = (x >> i) & 1;
    int bj = (x >> j) & 1;
    if (bi == bj) return x;
    int cleared = x & ~((1 << i) | (1 << j));
    int res = cleared | (bj << i) | (bi << j);
    return res;
  }
}
1
2
3
4
5
6
7
8
9
class Solution:
    def swap_bits_explicit(self, x: int, i: int, j: int) -> int:
        bi = (x >> i) & 1
        bj = (x >> j) & 1
        if bi == bj:
            return x
        cleared = x & ~(((1 << i) | (1 << j)))
        res = cleared | (bj << i) | (bi << j)
        return res

Complexity

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