-
Notifications
You must be signed in to change notification settings - Fork 2
/
bit_swapping.cpp
57 lines (43 loc) · 1.08 KB
/
bit_swapping.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/****************************************************************************
File name: bit_swapping.cpp
Author: babajr
*****************************************************************************/
#include<stdio.h>
int swap_bits(int n)
{
int res = 0;
res = ((n & 0xAAAAAAAA) >> 1) | ((n & 0x55555555) << 1);
return res;
}
int swap_two_bits(int n)
{
int res = 0;
res = ((n & 0xCCCCCCCC) >> 2) | ((n & 0x33333333) << 2);
return res;
}
int swap_nibble(int n)
{
int res = 0;
res = ((n & 0xf0f0f0f0) >> 4) | ((n & 0x0f0f0f0f) << 4);
return res;
}
int swap_octet(int n)
{
int res = 0;
res = ((n & 0xff00ff00) >> 8) | ((n & 0x00ff00ff) << 8);
return res;
}
int main(void)
{
int num = 10;
int res;
res = swap_bits(num);
printf("Result after bits swapped: %d\n", res);
res = swap_two_bits(num);
printf("Result after two bits swapped: %d\n", res);
res = swap_nibble(num);
printf("Result after nibble swapped: %d\n", res);
res = swap_octet(num);
printf("Result after octets swapped: %d\n", res);
return 0;
}