Swap bits at position i and j
EasyUpdated: Sep 30, 2025
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
Examples 1
Input: x = 0b10110, i = 1, j = 3
Output: 0b01110
Explanation: bits at positions 1 and 3 are different and get swapped.
Examples 2
Input: x = 0b1001, i = 0, j = 3
Output: 0b1001
Explanation: bits are the same, so x is unchanged.
Solution
Method 1 — Flip mask when bits differ (recommended)
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) & 1andbj = (x >> j) & 1. - If
bi != bj, computemask = (1 << i) | (1 << j)and returnx ^ mask. - Otherwise return
x.
Edge cases:
- Ensure
iandjare within the word width (e.g.,0 <= i,j < 32for 32-bit). Use unsigned shifts or guards where language requires.
Code
C++
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;
}
};
Go
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
}
Java
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;
}
}
Python
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
biandbjas in Method 1. - If
bi == bj, returnx. - Clear positions
iandjwithx & ~((1<<i)|(1<<j)). - Set bit
itobjand bitjtobiand return the result.
Code
C++
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;
}
};
Go
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
}
Java
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;
}
}
Python
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).