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
Explanation: 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 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++
#include<bits/stdc++.h>
using namespace std;
int swapBits(unsigned int n, unsigned int p1, unsigned int p2)
{
unsigned int bit1 = (n >> p1) & 1;
unsigned int bit2 = (n >> p2) & 1;
unsigned int x = (bit1 ^ bit2);
x = (x << p1) | (x << p2);
unsigned int result = n ^ x;
}
int main()
{
int res = swapBits(28, 0, 3);
cout<< "Result = " << res<< " " ;
return 0;
}
|
C
#include<stdio.h>
int swapBits(unsigned int n, unsigned int p1, unsigned int p2)
{
unsigned int bit1 = (n >> p1) & 1;
unsigned int bit2 = (n >> p2) & 1;
unsigned int x = (bit1 ^ bit2);
x = (x << p1) | (x << p2);
unsigned int result = n ^ x;
}
int main()
{
int res = swapBits(28, 0, 3);
printf ( "Result = %d " , res);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int swapBits( int n, int p1, int p2)
{
int bit1 = (n >> p1) & 1 ;
int bit2 = (n >> p2) & 1 ;
int x = (bit1 ^ bit2);
x = (x << p1) | (x << p2);
int result = n ^ x;
return result;
}
public static void main (String[] args)
{
int res = swapBits( 28 , 0 , 3 );
System.out.println ( "Result = " + res);
}
}
|
C#
using System;
class GFG
{
static int swapBits( int n, int p1, int p2)
{
int bit1 = (n >> p1) & 1;
int bit2 = (n >> p2) & 1;
int x = (bit1 ^ bit2);
x = (x << p1) | (x << p2);
int result = n ^ x;
return result;
}
public static void Main( string [] args)
{
int res = swapBits(28, 0, 3);
Console.Write( "Result = " + res);
}
}
|
Javascript
<script>
function swapBits(n , p1 , p2)
{
var bit1 = (n >> p1) & 1;
var bit2 = (n >> p2) & 1;
var x = (bit1 ^ bit2);
x = (x << p1) | (x << p2);
var result = n ^ x;
return result;
}
var res = swapBits(28, 0, 3);
document.write( "Result = " + res);
</script>
|
Python3
def swapBits(n, p1, p2):
bit1 = (n >> p1) & 1
bit2 = (n >> p2) & 1
x = (bit1 ^ bit2)
x = (x << p1) | (x << p2)
result = n ^ x
return result
if __name__ = = '__main__' :
res = swapBits( 28 , 0 , 3 )
print ( "Result = " , res)
|
Time Complexity: O(1)
Auxiliary Space: O(1)
C++
#include<iostream>
using namespace std;
int swapBits( int n, int p1, int p2)
{
if (((n & (1 << p1)) >> p1) ^ ((n & (1 << p2)) >> p2))
{
n ^= 1 << p1;
n ^= 1 << p2;
}
return n;
}
int main()
{
cout << "Result = " << swapBits(28, 0, 3);
return 0;
}
|
C
#include<stdio.h>
int swapBits( int n, int p1, int p2)
{
if (((n & (1 << p1)) >> p1) ^ ((n & (1 << p2)) >> p2))
{
n ^= 1 << p1;
n ^= 1 << p2;
}
return n;
}
int main()
{
printf ( "Result = %d" , swapBits(28, 0, 3));
return 0;
}
|
Java
import java.util.*;
class Main{
public static int swapBits( int n,
int p1,
int p2)
{
int temp = ((n & ( 1 << p1)) >> p1) ^ ((n & ( 1 << p2)) >> p2);
if (temp >= 1 )
{
n ^= 1 << p1;
n ^= 1 << p2;
}
return n;
}
public static void main(String[] args)
{
System.out.print( "Result = " +
swapBits( 28 , 0 , 3 ));
}
}
|
Python
def swapBits(n, p1, p2):
if ((n & ( 1 << p1)) >> p1) ^ ((n & ( 1 << p2)) >> p2):
n ^ = 1 << p1
n ^ = 1 << p2
return n
print ( "Result =" ,swapBits( 28 , 0 , 3 ))
|
C#
using System;
class GFG {
static int swapBits( int n, int p1, int p2)
{
int temp = ((n & (1 << p1)) >> p1) ^ ((n & (1 << p2)));
if (temp >= 1)
{
n ^= 1 << p1;
n ^= 1 << p2;
}
return n;
}
static void Main()
{
Console.WriteLine( "Result = " + swapBits(28, 0, 3));
}
}
|
Javascript
<script>
function swapBits(n, p1, p2)
{
temp = ((n & (1 << p1)) >> p1) ^ ((n & (1 << p2)) >> p2);
if (temp >= 1)
{
n ^= 1 << p1;
n ^= 1 << p2;
}
return n;
}
document.write( "Result = " + swapBits(28, 0, 3));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above