-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathswap bits
More file actions
36 lines (28 loc) · 823 Bytes
/
Copy pathswap bits
File metadata and controls
36 lines (28 loc) · 823 Bytes
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
34
35
36
// C++ Program to swap bits
// in a given number
#include <bits/stdc++.h>
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