Given an integer, swap two bits at given positions in binary representation of it.

For example,

**Input: **

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 same or not. If they are same, nothing needs to be done else if they are not the same (i.e one is 0 and other is 1), then we can simply XOR them with (1 << pos) where pos is given positions. This will work because

XOR with 1 will toggle the bits

0 ^ 1 = 1

1 ^ 1 = 0

XOR with 0 will have no impact

0 ^ 0 = 0

1 ^ 0 = 1

## 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 28 |
#include <iostream> #include <bitset> using namespace std; // Function to swap bits are 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; } // main function int main() { int n = 31; int p = 2, q = 6; // (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 29 30 |
class Swap { // Function to swap bits are 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) throws java.lang.Exception { int n = 31; // swap 3rd and 7th bit from the right int p = 2, q = 6; 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

As pointed out by reader in comments below, we could hoist (1 << p) and (1 << q) into a variable to avoid doing repeated work. We can also remove the conditional check and do the swap in any case to avoid unnecessary memory reads as shown below –

1 2 3 4 5 6 7 8 |
// Swap bits are position p and q in integer n int swap(int n, int p, int q) { n ^= (1 << p); n ^= (1 << q); return n; } |

**Thanks for reading.**

Please use ideone or C++ Shell or any other online compiler link to post code in comments.

Like us? Please spread the word and help us grow. Happy coding 🙂

## Leave a Reply

Check output of your program for

`p = 2`

and`q = 4`

.I think in swap() function p and q should be:

`p = 8 - p;`

q = 8 - q;

Sameer, code seems to be fine. It will swap 3rd and 5th bit from the right (LSB) which happens to be same. Please check –

https://ideone.com/BB8Lya

For p = 8 – p and q = 8 – q, it will swap 2nd and 4th bit from the left side (MSB) of 8-bit integer.

Minor improvement suggestions:

You could hoist (1 << p) and (1 << q) into a variable to not have to do it twice. Would also help with readability.

Even better you can actually remove the check and do the swap in any case. The check just results in unnecessary memory reads.

Hey, thanks a lot for suggesting this. We have updated the article. Happy Coding 🙂