Given a number x and two positions (from the right side) in the binary representation of x, write a function that swaps n bits at the given two positions and returns the result. It is also given that the two sets of bits do not overlap.
Method 1
Let p1 and p2 be the two given positions.
Example 1
Input:
x = 47 (00101111)
p1 = 1 (Start from the second bit from the right side)
p2 = 5 (Start from the 6th bit from the right side)
n = 3 (No of bits to be swapped)
Output:
227 (11100011)
The 3 bits starting from the second bit (from the right side) are
swapped with 3 bits starting from 6th position (from the 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 position (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 the first set to the 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++
#include <bits/stdc++.h>
using namespace std;
int swapBits(unsigned int x, unsigned int p1,
unsigned int p2, unsigned int n)
{
unsigned int set1 = (x >> p1) & ((1U << n) - 1);
unsigned int set2 = (x >> p2) & ((1U << n) - 1);
unsigned int Xor = (set1 ^ set2);
Xor = (Xor << p1) | (Xor << p2);
unsigned int result = x ^ Xor;
return result;
}
int main()
{
int res = swapBits(28, 0, 3, 2);
cout << "Result = " << res;
return 0;
}
|
C
#include <stdio.h>
int swapBits(unsigned int x, unsigned int p1, unsigned int p2, unsigned int n)
{
unsigned int set1 = (x >> p1) & ((1U << n) - 1);
unsigned int set2 = (x >> p2) & ((1U << n) - 1);
unsigned int xor = (set1 ^ set2);
xor = (xor << p1) | (xor << p2);
unsigned int result = x ^ xor;
return result;
}
int main()
{
int res = swapBits(28, 0, 3, 2);
printf ( "\nResult = %d " , res);
return 0;
}
|
Java
class GFG {
static int swapBits( int x, int p1, int p2, int n)
{
int set1 = (x >> p1) & (( 1 << n) - 1 );
int set2 = (x >> p2) & (( 1 << n) - 1 );
int xor = (set1 ^ set2);
xor = (xor << p1) | (xor << p2);
int result = x ^ xor;
return result;
}
public static void main(String[] args)
{
int res = swapBits( 28 , 0 , 3 , 2 );
System.out.println( "Result = " + res);
}
}
|
Python3
def swapBits(x, p1, p2, n):
set1 = (x >> p1) & (( 1 << n) - 1 )
set2 = (x >> p2) & (( 1 << n) - 1 )
xor = (set1 ^ set2)
xor = (xor << p1) | (xor << p2)
result = x ^ xor
return result
res = swapBits( 28 , 0 , 3 , 2 )
print ( "Result =" , res)
|
C#
using System;
class GFG {
static int swapBits( int x, int p1, int p2, int n)
{
int set1 = (x >> p1) & ((1 << n) - 1);
int set2 = (x >> p2) & ((1 << n) - 1);
int xor = (set1 ^ set2);
xor = (xor << p1) | (xor << p2);
int result = x ^ xor;
return result;
}
public static void Main()
{
int res = swapBits(28, 0, 3, 2);
Console.WriteLine( "Result = " + res);
}
}
|
PHP
<?php
function swapBits( $x , $p1 , $p2 , $n )
{
$set1 = ( $x >> $p1 ) &
((1 << $n ) - 1);
$set2 = ( $x >> $p2 ) &
((1 << $n ) - 1);
$xor = ( $set1 ^ $set2 );
$xor = ( $xor << $p1 ) |
( $xor << $p2 );
$result = $x ^ $xor ;
return $result ;
}
$res = swapBits(28, 0, 3, 2);
echo "\nResult = " , $res ;
?>
|
Javascript
<script>
function swapBits(x, p1, p2, n)
{
let set1 = (x >> p1) & ((1 << n) - 1);
let set2 = (x >> p2) & ((1 << n) - 1);
let xor = (set1 ^ set2);
xor = (xor << p1) | (xor << p2);
let result = x ^ xor;
return result;
}
let res = swapBits(28, 0, 3, 2);
document.write( "Result = " + res);
</script>
|
Time Complexity: O(1), as we are using constant-time operations.
Auxiliary Space: O(1), as we are not using any extra space.
Following is a shorter implementation of the same logic
C++
#include <bits/stdc++.h>
using namespace std;
int swapBits( int x, int p1, int p2, int n)
{
int xor = ((x >> p1) ^ (x >> p2)) & ((1U << n) - 1);
return x ^ ((xor << p1) | (xor << p2));
}
|
C
int swapBits(unsigned int x, unsigned int p1, unsigned int p2, unsigned int n)
{
unsigned int xor = ((x >> p1) ^ (x >> p2)) & ((1U << n) - 1);
return x ^ ( (xor << p1) | (xor << p2));
}
|
Java
static int swapBits( int x, int p1, int p2, int n)
{
int xor = ((x >> p1) ^ (x >> p2)) & ((1U << n) - 1 );
return x ^ ((xor << p1) | (xor << p2));
}
|
Python3
def swapBits(x, p1, p2, n) :
xor = (((x >> p1) ^ (x >> p2)) & (( 1 << n) - 1 ))
return x ^ ( (xor << p1) | (xor << p2))
|
C#
static int swapBits( int x, int p1, int p2, int n)
{
int xor = ((x >> p1) ^ (x >> p2)) & ((1U << n) - 1);
return x ^ ((xor << p1) | (xor << p2));
}
|
Javascript
<script>
function swapBits(x, p1, p2, n)
{
let xor = ((x >> p1) ^ (x >> p2)) & ((1 << n) - 1);
return x ^ ((xor << p1) | (xor << p2));
}
</script>
|
Time Complexity: O(1), as we are using constant-time operations.
Auxiliary Space: O(1), as we are not using any extra space.
Method 2 –
This solution focuses on calculating the values of bits to be swapped using AND gate. Then we can set/unset those bits based on whether the bits are to be swapped. For the number of bits to be swapped (n) –
- Calculate shift1 = The value after setting bit at p1 position to 1
- Calculate shift2 = The value after setting bit at p2 position to 1
- value1 = Number to check if num at position p1 is set or not.
- value2 = Number to check if num at position p2 is set or not.
- If value1 and value2 are different is when we have to swap the bits.
Example:
[28 0 3 2] num=28 (11100) p1=0 p2=3 n=2
Given = 11100
Required output = 00111 i.e. (00)1(11) msb 2 bits replaced with lsb 2 bits
n=2
p1=0, p2=3
shift1= 1, shift2= 1000
value1= 0, value2= 1000
After swap
num= 10101
n=3
p1=1, p2=4
shift1= 10, shift2= 10000
value1= 0, value2= 10000
After swap
num= 00111
Implementation
C++
#include <iostream>
using namespace std;
int swapBits(unsigned int num, unsigned int p1,
unsigned int p2, unsigned int n)
{
int shift1, shift2, value1, value2;
while (n--) {
shift1 = 1 << p1;
shift2 = 1 << p2;
value1 = ((num & shift1));
value2 = ((num & shift2));
if ((!value1 && value2) || (!value2 && value1)) {
if (value1) {
num = num & (~shift1);
num = num | shift2;
}
else {
num = num & (~shift2);
num = num | shift1;
}
}
p1++;
p2++;
}
return num;
}
int main()
{
int res = swapBits(28, 0, 3, 2);
cout << "Result = " << res;
return 0;
}
|
Java
class GFG
{
static int swapBits( int num, int p1, int p2, int n)
{
int shift1, shift2, value1, value2;
while (n-- > 0 )
{
shift1 = 1 << p1;
shift2 = 1 << p2;
value1 = ((num & shift1));
value2 = ((num & shift2));
if ((value1 == 0 && value2 != 0 ) ||
(value2 == 0 && value1 != 0 ))
{
if (value1 != 0 )
{
num = num & (~shift1);
num = num | shift2;
}
else
{
num = num & (~shift2);
num = num | shift1;
}
}
p1++;
p2++;
}
return num;
}
public static void main(String[] args)
{
int res = swapBits( 28 , 0 , 3 , 2 );
System.out.println( "Result = " + res);
}
}
|
Python3
def swapBits(num, p1, p2, n):
shift1 = 0
shift2 = 0
value1 = 0
value2 = 0
while (n > 0 ):
shift1 = 1 << p1
shift2 = 1 << p2
value1 = ((num & shift1))
value2 = ((num & shift2))
if ((value1 = = 0 and value2 ! = 0 ) or (value2 = = 0 and value1 ! = 0 )):
if (value1 ! = 0 ):
num = num & (~shift1)
num = num | shift2
else :
num = num & (~shift2)
num = num | shift1
p1 + = 1
p2 + = 1
n - = 1
return num
res = swapBits( 28 , 0 , 3 , 2 )
print ( "Result =" , res)
|
C#
using System;
class GFG
{
static int swapBits( int num, int p1,
int p2, int n)
{
int shift1, shift2, value1, value2;
while (n-- > 0)
{
shift1 = 1 << p1;
shift2 = 1 << p2;
value1 = ((num & shift1));
value2 = ((num & shift2));
if ((value1 == 0 && value2 != 0) || (value2 == 0 && value1 != 0))
{
if (value1 != 0)
{
num = num & (~shift1);
num = num | shift2;
}
else
{
num = num & (~shift2);
num = num | shift1;
}
}
p1++;
p2++;
}
return num;
}
static void Main()
{
int res = swapBits(28, 0, 3, 2);
Console.WriteLine( "Result = " + res);
}
}
|
Javascript
<script>
function swapBits(num, p1, p2, n)
{
let shift1, shift2, value1, value2;
while (n-- > 0)
{
shift1 = 1 << p1;
shift2 = 1 << p2;
value1 = ((num & shift1));
value2 = ((num & shift2));
if ((value1 == 0 && value2 != 0) ||
(value2 == 0 && value1 != 0))
{
if (value1 != 0)
{
num = num & (~shift1);
num = num | shift2;
}
else
{
num = num & (~shift2);
num = num | shift1;
}
}
p1++;
p2++;
}
return num;
}
let res = swapBits(28, 0, 3, 2);
document.write( "Result = " + res);
</script>
|
Time Complexity: O(N), as we are using a loop to traverse N times. Where N is the number of bits to be swapped.
Auxiliary Space: O(1), as we are not using any extra space.
References:
Swapping individual bits with XOR
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.