/
2.7_invert.c
65 lines (48 loc) · 1.1 KB
/
2.7_invert.c
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
58
59
60
61
62
63
64
65
/* 2.7 write a function invert(x,p,n) that returns x with
* the n bits that begin at position p inverted. */
#include <stdlib.h>
#include <stdio.h>
#define unused(x) (void)x
unsigned int get_bits(int num) {
if (num == 1 || num == 0 ) {
return num;
}
return (num%2 + get_bits((num/2))*10);
}
int stoi(char* string)
{
return (int) strtol(string, NULL, 10);
}
int get_mask(int p,int n) {
return ~((~0) << n) << p;
}
int main (int argc, char* argv[]) {
int min_args = 3;
if (argc < min_args+1) {
printf("need at least %d arguments.\n",min_args);
exit(EXIT_FAILURE);
}
int x, p, n;
x = stoi(argv[1]);
p = stoi(argv[2]);
n = stoi(argv[3]);
printf("starting with %d -> %d\n",x,get_bits(x));
int mask = get_mask(n, p);
printf("generating mask %d\n",get_bits(mask));
int masked_x = x ^ mask;
printf("x after XOR'ing with the mask: %d\n",get_bits(masked_x));
}
/*
5 4 3 2 1 0
-----------
0 0 0 0 0 0
xor?
1 0 1 1 0 0 with n=3 and p=1
-->
1 1 0 0 0 0
--
1 0 1 1 0 0
0 1 1 1 0 0 xor
----------------
1 1 0 0 0 0
*/