# Swap bits in a given number

Given a number x and two positions (from right side) in binary representation of x, write a function that swaps n bits at given two positions and returns the result. It is also given that the two sets of bits do not overlap.

Let p1 and p2 be the two given positions.

Example 1
Input:
x = 47 (00101111)
p1 = 1 (Start from second bit from right side)
p2 = 5 (Start from 6th bit from right side)
n = 3 (No of bits to be swapped)
Output:
227 (11100011)
The 3 bits starting from the second bit (from right side) are
swapped with 3 bits starting from 6th position (from right side)

Example 2
Input:
x = 28 (11100)
p1 = 0 (Start from first bit from right side)
p2 = 3 (Start from 4th bit from right side)
n = 2 (No of bits to be swapped)
Output:
7 (00111)
The 2 bits starting from 0th postion (from right side) are
swapped with 2 bits starting from 4th position (from right side)

Solution
We need to swap two sets of bits. XOR can be used in a similar way as it is used to swap 2 numbers. Following is the algorithm.

```1) Move all bits of first set to rightmost side
set1 =  (x >> p1) & ((1U << n) - 1)
Here the expression (1U << n) - 1 gives a number that
contains last n bits set and other bits as 0. We do &
with this expression so that bits other than the last
n bits become 0.
2) Move all bits of second set to rightmost side
set2 =  (x >> p2) & ((1U << n) - 1)
3) XOR the two sets of bits
xor = (set1 ^ set2)
4) Put the xor bits back to their original positions.
xor = (xor << p1) | (xor << p2)
5) Finally, XOR the xor with original number so
that the two sets are swapped.
result = x ^ xor
```

Implementation:

## C++

 `// C++ Program to swap bits  ` `// in a given number  ` `#include ` `using` `namespace` `std; ` ` `  `int` `swapBits(unsigned ``int` `x, unsigned ``int` `p1,  ` `            ``unsigned ``int` `p2, unsigned ``int` `n)  ` `{  ` `    ``/* Move all bits of first set to rightmost side */` `    ``unsigned ``int` `set1 = (x >> p1) & ((1U << n) - 1);  ` ` `  `    ``/* Move all bits of second set to rightmost side */` `    ``unsigned ``int` `set2 = (x >> p2) & ((1U << n) - 1);  ` ` `  `    ``/* Xor the two sets */` `    ``unsigned ``int` `Xor = (set1 ^ set2);  ` ` `  `    ``/* Put the Xor bits back to their original positions */` `    ``Xor = (Xor << p1) | (Xor << p2);  ` ` `  `    ``/* Xor the 'Xor' with the original number so that the  ` `    ``two sets are swapped */` `    ``unsigned ``int` `result = x ^ Xor;  ` ` `  `    ``return` `result;  ` `}  ` ` `  `/* Driver code*/` `int` `main()  ` `{  ` `    ``int` `res = swapBits(28, 0, 3, 2);  ` `    ``cout << ``"Result = "` `<< res;  ` `    ``return` `0;  ` `}  ` ` `  `// This code is contributed by rathbhupendra `

## C

 `// C Program to swap bits  ` `// in a given number ` `#include ` ` `  `int` `swapBits(unsigned ``int` `x, unsigned ``int` `p1, unsigned ``int` `p2, unsigned ``int` `n) ` `{ ` `    ``/* Move all bits of first set to rightmost side */` `    ``unsigned ``int` `set1 =  (x >> p1) & ((1U << n) - 1); ` ` `  `    ``/* Move all bits of second set to rightmost side */` `    ``unsigned ``int` `set2 =  (x >> p2) & ((1U << n) - 1); ` ` `  `    ``/* XOR the two sets */` `    ``unsigned ``int` `xor = (set1 ^ set2); ` ` `  `    ``/* Put the xor bits back to their original positions */` `    ``xor = (xor << p1) | (xor << p2); ` ` `  `    ``/* XOR the 'xor' with the original number so that the  ` `       ``two sets are swapped */` `    ``unsigned ``int` `result = x ^ xor; ` ` `  `    ``return` `result; ` `} ` ` `  `/* Driver program to test above function*/` `int` `main() ` `{ ` `    ``int` `res =  swapBits(28, 0, 3, 2); ` `    ``printf``(````" Result = %d "````, res); ` `    ``return` `0; ` `} `

## Java

 `//Java Program to swap bits  ` `// in a given number ` ` `  `class` `GFG { ` `     `  `    ``static` `int` `swapBits(``int` `x, ``int` `p1, ``int` `p2, ``int` `n) ` `    ``{ ` `        ``// Move all bits of first set ` `        ``// to rightmost side  ` `        ``int` `set1 = (x >> p1) & ((``1` `<< n) - ``1``); ` `     `  `        ``// Move all bits of second set  ` `        ``//to rightmost side  ` `        ``int` `set2 = (x >> p2) & ((``1` `<< n) - ``1``); ` `     `  `        ``// XOR the two sets  ` `        ``int` `xor = (set1 ^ set2); ` `     `  `        ``// Put the xor bits back to  ` `        ``// their original positions  ` `        ``xor = (xor << p1) | (xor << p2); ` `     `  `        ``// XOR the 'xor' with the original number  ` `        ``// so that the  two sets are swapped  ` `        ``int` `result = x ^ xor; ` `     `  `        ``return` `result; ` `    ``} ` `     `  `    ``// Driver program  ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `res = swapBits(``28``, ``0``, ``3``, ``2``); ` `        ``System.out.println(``"Result = "` `+ res); ` `    ``} ` `} ` ` `  `// This code is contributed by prerna saini. `

## Python3

 `# Python program to ` `# swap bits in a given number ` ` `  `def` `swapBits(x,p1,p2,n): ` ` `  `    ``# Move all bits of first ` `    ``# set to rightmost side  ` `    ``set1 ``=`  `(x >> p1) & ((``1``<< n) ``-` `1``) ` `  `  `    ``# Moce all bits of second ` `    ``# set to rightmost side  ` `    ``set2 ``=`  `(x >> p2) & ((``1` `<< n) ``-` `1``) ` `  `  `    ``# XOR the two sets  ` `    ``xor ``=` `(set1 ^ set2) ` `  `  `    ``# Put the xor bits back ` `    ``# to their original positions  ` `    ``xor ``=` `(xor << p1) | (xor << p2) ` `  `  `      ``# XOR the 'xor' with the ` `      ``# original number so that the  ` `      ``# two sets are swapped ` `    ``result ``=` `x ^ xor ` `  `  `    ``return` `result ` `     `  `# Driver code ` ` `  `res ``=``swapBits(``28``, ``0``, ``3``, ``2``) ` `print``(``"Result ="``,res) ` ` `  `# This code is contributed ` `# by Anant Agarwal. `

## C#

 `// C# Program to swap bits  ` `// in a given number ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``static` `int` `swapBits(``int` `x, ``int` `p1, ``int` `p2, ``int` `n) ` `    ``{ ` `        ``// Move all bits of first  ` `        ``//set to rightmost side  ` `        ``int` `set1 = (x >> p1) & ((1 << n) - 1); ` `     `  `        ``// Move all bits of second set  ` `        ``// set to rightmost side  ` `        ``int` `set2 = (x >> p2) & ((1 << n) - 1); ` `     `  `        ``// XOR the two sets  ` `        ``int` `xor = (set1 ^ set2); ` `     `  `        ``// Put the xor bits back to  ` `        ``// their original positions  ` `        ``xor = (xor << p1) | (xor << p2); ` `     `  `        ``// XOR the 'xor' with the original number  ` `        ``// so that the two sets are swapped  ` `        ``int` `result = x ^ xor; ` `     `  `        ``return` `result; ` `    ``} ` `     `  `    ``// Driver program  ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `res = swapBits(28, 0, 3, 2); ` `        ``Console.WriteLine(``"Result = "` `+ res); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 `> ``\$p1``) &  ` `            ``((1 << ``\$n``) - 1); ` ` `  `    ``// Move all bits of second ` `    ``// set to rightmost side  ` `    ``\$set2` `= (``\$x` `>> ``\$p2``) &  ` `            ``((1 << ``\$n``) - 1); ` ` `  `    ``// XOR the two sets  ` `    ``\$xor` `= (``\$set1` `^ ``\$set2``); ` ` `  `    ``// Put the xor bits back to  ` `    ``// their original positions  ` `    ``\$xor` `= (``\$xor` `<< ``\$p1``) |  ` `           ``(``\$xor` `<< ``\$p2``); ` ` `  `    ``// XOR the 'xor' with the  ` `    ``// original number so that ` `    ``// the two sets are swapped  ` `    ``\$result` `= ``\$x` `^ ``\$xor``; ` ` `  `    ``return` `\$result``; ` `} ` ` `  `    ``// Driver Code ` `    ``\$res` `= swapBits(28, 0, 3, 2); ` `    ``echo` ```" Result = "````, ``\$res``; ` `     `  `// This code is contributed by anuj_67. ` `?> `

Output:

``` Result = 7
```

Following is a shorter implementation of the same logic

 `int` `swapBits(unsigned ``int` `x, unsigned ``int` `p1, unsigned ``int` `p2, unsigned ``int` `n) ` `{ ` `    ``/* xor contains xor of two sets */` `    ``unsigned ``int` `xor = ((x >> p1) ^ (x >> p2)) & ((1U << n) - 1); ` ` `  `    ``/* To swap two sets, we need to again XOR the xor with original sets */` `    ``return` `x ^ ((xor << p1) | (xor << p2)); ` `} `

References:
Swapping individual bits with XOR

## tags:

Bit Magic Bitwise-XOR Bit Magic