-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise_2-7.c
More file actions
43 lines (31 loc) · 967 Bytes
/
exercise_2-7.c
File metadata and controls
43 lines (31 loc) · 967 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
37
38
39
40
41
42
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <stdio.h>
#include <stdbool.h>
unsigned getbits(unsigned x, int p, int n) {
return (x >> (p-1+n))&~(~0<<n);
}
unsigned invert(unsigned x, int p, int n) {
int pos = p; bool bit_at_p = 0;
while (pos >= (p-n+1)) {
if ( getbits(x,pos,1) == 1)
{
x&=~(1<<pos);
}
else // getbits(x,pos,0) == 0
{
x |= (1 << pos);
}
pos--;
}
return x;
}
int main()
{
printf("%u\n",invert(~0,31,32));
return 0;
}