-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset_bits.c
More file actions
36 lines (33 loc) · 765 Bytes
/
Copy pathset_bits.c
File metadata and controls
36 lines (33 loc) · 765 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
#include <stdio.h>
#include "bitcount.h"
int bitcount(int x);
int setbits(int x, int p, int n, int y);
/*
return x with n bits that begin at position p
set to the rightmost n bits of y
leaving the others unchanged
*/
int main(){
int n1, p, n, n2;
int c, flag = 0;
while((c = getchar()) != EOF && c != '\n'){
if(c != ' '){
if(!n1)
n1 = c;
else if(!p)
p = c;
else if (!n)
n = c;
else if (!n2)
n2 = c;
}
}
printf("%d",setbits(n1, p, n, n2));
}
int setbits(int x, int p, int n, int y){
int y_count = bitcount(y);
int diff = y_count - n;
y &= ~(~0 << (y_count - diff));
y <<= (p-1);
return x & y;
}