Swap two bits at a given position in an integer
Given an integer, swap two bits at given positions in a binary representation of it.
For example,
n = 31 (31 in binary is 00011111)
p = 2, q = 6 (3rd and 7th bit from the right)
Output: 91
Explanation: 91 in binary is 01011011
We can solve this problem by checking if the two bits at given positions are the same or not. If they are the same, nothing needs to be done; otherwise, if they are not the same (i.e., one is 0 and the other is 1), then we can XOR them with 1 << position
. This logic will work because:
0 ^ 1 = 1
1 ^ 1 = 0
XOR with 0 will have no impact
0 ^ 0 = 0
1 ^ 0 = 1
Following is the C++, Java, and Python implementation of the idea:
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> #include <bitset> using namespace std; // Function to swap bits at position `p` and `q` in integer `n` int swap(int n, int p, int q) { // if bits are different at position `p` and `q` if (((n & (1 << p)) >> p) ^ ((n & (1 << q)) >> q)) { n ^= (1 << p); n ^= (1 << q); } return n; } int main() { int n = 31; int p = 2, q = 6; // swap 3rd and 7th bit from the right cout << n << " in binary is " << bitset<8>(n) << endl; n = swap (n, p, q); cout << n << " in binary is " << bitset<8>(n) << endl; return 0; } |
Output:
31 in binary is 00011111
91 in binary is 01011011
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
class Main { // Function to swap bits at position `p` and `q` in integer `n` public static int swap(int n, int p, int q) { // if bits are different at position `p` and `q` if ((((n & (1 << p)) >> p) ^ ((n & (1 << q)) >> q)) == 1) { n ^= (1 << p); n ^= (1 << q); } return n; } public static void main (String[] args) { int n = 31; int p = 2, q = 6; // swap 3rd and 7th bit from the right System.out.println(n + " in binary is " + String.format("%08d", Integer.parseInt(Integer.toBinaryString(n)))); n = swap (n, p, q); System.out.println(n + " in binary is " + String.format("%08d", Integer.parseInt(Integer.toBinaryString(n)))); } } |
Output:
31 in binary is 00011111
91 in binary is 01011011
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# Function to swap bits at position `p` and `q` in integer `n` def swap(n, p, q): # if bits are different at position `p` and `q` if (((n & (1 << p)) >> p) ^ ((n & (1 << q)) >> q)) == 1: n ^= (1 << p) n ^= (1 << q) return n if __name__ == '__main__': n = 31 # swap 3rd and 7th bit from the right p = 2 q = 6 print(f'{n} in binary is', bin(n)[2:].zfill(8)) n = swap(n, p, q) print(f'{n} in binary is', bin(n)[2:].zfill(8)) |
Output:
31 in binary is 00011111
91 in binary is 01011011
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)