-
Notifications
You must be signed in to change notification settings - Fork 0
/
setbits.c
42 lines (33 loc) · 1.15 KB
/
setbits.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
/* Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n
* bits of y, leaving the other bits unchanged. */
#include "stdio.h"
unsigned setbits(unsigned x, unsigned p, unsigned n, unsigned y);
unsigned digit(unsigned num);
int main(void) {
unsigned x = 5514; // 1 0101 1000 1010
unsigned p = 4; // right-adjusted, starting at 0
unsigned n = 19;
unsigned y = 1885; // 0111 0101 1101
/* expect 1 0111 1010 1010 = 6058 */
printf("%u\n", setbits(x, p, n, y));
}
unsigned setbits(unsigned x, unsigned p, unsigned n, unsigned y) {
if (p < n)
perror("`p` shouldn't less than `n`");
if (digit(x) < p + 1) {
printf("`x`'s digit: %u\n", digit(x));
perror("`x`'s digit isn't fit for `p`");
}
if (digit(y) < n) {
printf("`y`'s digit: %u\n", digit(y));
perror("`y`'s digit isn't fit for `n`");
}
return x & ~(~(~(unsigned)0 << n) << (p-n+1)) | (y & ~(~(unsigned)0 << n)) << (p-n+1);
}
/* Return an unsigned integer's digits */
unsigned digit(unsigned num) {
unsigned i;
for (i = 0; num % 2 != 0 || num / 2 != 0; ++i)
num /= 2;
return i;
}