Given an integer n and two-bit positions p1 and p2 inside it, swap bits at the given positions. The given positions are from the least significant bit (lsb). For example, the position for lsb is 0.**Examples:**

Input: n = 28, p1 = 0, p2 = 3 Output: 21 28 in binary is 11100. If we swap 0'th and 3rd digits, we get 10101 which is 21 in decimal. Input: n = 20, p1 = 2, p2 = 3 Output: 24

**We strongly recommend you to minimize your browser and try this yourself first.****Method 1:**

The idea is to first find the bits, then use XOR based swapping concept, i..e., to swap two numbers ‘x’ and ‘y’, we do x = x ^ y, y = y ^ x and x = x ^ y.

Below is the implementation of the above idea

C++

// C program to swap bits in an integer #include<stdio.h> // This function swaps bit at positions p1 and p2 in an integer n int swapBits(unsigned int n, unsigned int p1, unsigned int p2) { /* Move p1'th to rightmost side */ unsigned int bit1 = (n >> p1) & 1; /* Move p2'th to rightmost side */ unsigned int bit2 = (n >> p2) & 1; /* XOR the two bits */ unsigned int x = (bit1 ^ bit2); /* Put the xor bit back to their original positions */ x = (x << p1) | (x << p2); /* XOR 'x' with the original number so that the two sets are swapped */ unsigned int result = n ^ x; } /* Driver program to test above function*/ int main() { int res = swapBits(28, 0, 3); printf("Result = %d ", res); return 0; }

## Java

`// Java program to swap bits in an integer` `import` `java.io.*;` `class` `GFG ` `{` ` ` `// This function swaps bit at ` `// positions p1 and p2 in an integer n` `static` `int` `swapBits( ` `int` `n, ` `int` `p1, ` `int` `p2)` `{` ` ` `/* Move p1'th to rightmost side */` ` ` `int` `bit1 = (n >> p1) & ` `1` `;` ` ` `/* Move p2'th to rightmost side */` ` ` `int` `bit2 = (n >> p2) & ` `1` `;` ` ` `/* XOR the two bits */` ` ` `int` `x = (bit1 ^ bit2);` ` ` `/* Put the xor bit back to` ` ` `their original positions */` ` ` `x = (x << p1) | (x << p2);` ` ` `/* XOR 'x' with the original` ` ` `number so that the` ` ` `two sets are swapped */` ` ` `int` `result = n ^ x;` ` ` `return` `result;` `}` ` ` `/* Driver code*/` ` ` `public` `static` `void` `main (String[] args)` ` ` `{` ` ` `int` `res = swapBits(` `28` `, ` `0` `, ` `3` `);` ` ` `System.out.println (` `"Result = "` `+ res);` ` ` `}` `}` `// This code is contributed by ajit..` |

*chevron_right*

*filter_none*

Python3

```
# Python3 program to swap bits in an integer
# This function swaps bit at positions
# p1 and p2 in an integer n
def swapBits(n, p1, p2):
''' Move p1'th to rightmost side '''
bit1 = (n >> p1) & 1
''' Move p2'th to rightmost side '''
bit2 = (n >> p2) & 1
''' XOR the two bits '''
x = (bit1 ^ bit2)
''' Put the xor bit back to their
original positions '''
x = (x << p1) | (x << p2)
''' XOR 'x' with the original number
so that thetwo sets are swapped '''
result = n ^ x
return result
# Driver Code
res = swapBits(28, 0, 3)
print("Result =", res)
# This code is contributed by SHUBHAMSINGH10
```

C#

```
// C# program to swap bits in an integer
using System;
class GFG
{
// This function swaps bit at positions
// p1 and p2 in an integer n
static int swapBits(int n, int p1, int p2)
{
/* Move p1'th to rightmost side */
int bit1 = (n >> p1) & 1;
/* Move p2'th to rightmost side */
int bit2 = (n >> p2) & 1;
/* XOR the two bits */
int x = (bit1 ^ bit2);
/* Put the xor bit back to
their original positions */
x = (x << p1) | (x << p2);
/* XOR 'x' with the original
number so that the
two sets are swapped */
int result = n ^ x;
return result;
}
/* Driver code*/
static public void Main ()
{
int res = swapBits(28, 0, 3);
Console.WriteLine("Result = " + res);
}
}
// This code is contributed by akt_mit
```

## C++

`//C++ code for swapping given bits of a number` `#include<iostream>` `using` `namespace` `std;` `int` `swapBits(` `int` `n, ` `int` `p1, ` `int` `p2)` `{` ` ` `//left-shift 1 p1 and p2 times ` ` ` `//and using XOR` ` ` `n ^= 1 << p1;` ` ` `n ^= 1 << p2;` ` ` `return` `n;` `}` `//Driver Code` `int` `main()` `{` ` ` `cout << ` `"Result = "` `<< swapBits(28, 0, 3);` ` ` `return` `0;` `}` `//This code is contributed by yashbeersingh42` |

*chevron_right*

*filter_none*

## C

`//C code for swapping given bits of a number` `#include<stdio.h>` `int` `swapBits(` `int` `n, ` `int` `p1, ` `int` `p2)` `{` ` ` `//left-shift 1 p1 and p2 times ` ` ` `//and using XOR` ` ` `n ^= 1 << p1;` ` ` `n ^= 1 << p2;` ` ` `return` `n;` `}` `//Driver Code` `int` `main()` `{` ` ` `printf` `(` `"Result = %d"` `, swapBits(28, 0, 3));` ` ` `return` `0;` `}` `//This code is contributed by yashbeersingh42` |

*chevron_right*

*filter_none*

## Java

`// Java code for swapping ` `// given bits of a number ` `import` `java.util.*;` `class` `Main{` ` ` `public` `static` `int` `swapBits(` `int` `n, ` ` ` `int` `p1, ` ` ` `int` `p2) ` `{ ` ` ` `//left-shift 1 p1 and ` ` ` `// p2 times and using XOR ` ` ` `n ^= ` `1` `<< p1; ` ` ` `n ^= ` `1` `<< p2; ` ` ` `return` `n; ` `} ` `// Driver code` `public` `static` `void` `main(String[] args) ` `{` ` ` `System.out.print(` `"Result = "` `+ ` ` ` `swapBits(` `28` `, ` `0` `, ` `3` `));` `}` `}` `// This code is contributed by divyeshrabadiya07` |

*chevron_right*

*filter_none*

Output:Result = 21

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Swap every two bits in bytes
- Swap bits in a given number
- Check if all bits can be made same by flipping two consecutive bits
- Swap all odd and even bits
- Check if bits of a number has count of consecutive set bits in increasing order
- Toggle bits of a number except first and last bits
- Print numbers having first and last bits as the only set bits
- Count set bits in an integer
- Next greater integer having one more number of set bits
- Previous smaller integer having one less number of set bits
- Count set bits in an integer using Lookup Table
- Find the largest number smaller than integer N with maximum number of set bits
- Minimum integer with at most K bits set such that their bitwise AND with N is maximum
- Cyclic shifts of integer N by another integer m
- Swap two nibbles in a byte
- Number of ways to swap two bit of s1 so that bitwise OR of s1 and s2 changes
- How to swap two numbers without using a temporary variable?
- Numbers formed by flipping common set bits in two given integers
- Print first n numbers with exactly two set bits
- Alternate bits of two numbers to create a new number