forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swap.cpp
33 lines (25 loc) · 854 Bytes
/
swap.cpp
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
31
32
33
// C Program to swap bits
// in a given number
#include<stdio.h>
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 xo = (set1 ^ set2);
/* Put the xor bits back to their original positions */
xo = (xo << p1) | (xo << p2);
/* XOR the 'xor' with the original number so that the
two sets are swapped */
unsigned int result = x ^ xo;
return result;
}
/* Driver program to test above function*/
int main()
{
int res = swapBits(28, 0, 3, 2);
printf("\nResult = %d ", res);
return 0;
}