-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise_2-6.c
executable file
·65 lines (49 loc) · 1.66 KB
/
exercise_2-6.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
#include <stdio.h>
#include <string.h>
/* 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. */
#define TESTCOUNT 2 /* the number of tests */
unsigned int setbits(unsigned int x, int p, int n, unsigned int y);
/* helper for printing. http://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format */
const char *byte_to_binary (int x);
main() {
unsigned int x[TESTCOUNT] = { 0b0000, 0b1111 };
unsigned int y[TESTCOUNT] = { 0b1111, 0b0000 };
int p[TESTCOUNT] = { 2, 1 };
int n[TESTCOUNT] = { 2, 2 };
unsigned int expected[TESTCOUNT] = { 0b1100, 0b1001 };
unsigned int actual;
int i;
for (i=0; i<TESTCOUNT; i++) {
actual = setbits(x[i], p[i], n[i], y[i]);
if (actual == expected[i]) {
printf("Test %d : PASS\n", i);
} else {
printf("Test %d : FAIL\n", i);
printf(" x: %s\n", byte_to_binary(x[i]));
printf(" y: %s\n", byte_to_binary(y[i]));
printf(" p: %d, n: %d\n", p[i], n[i]);
printf(" expected: %s\n", byte_to_binary(expected[i]));
printf(" actual: %s\n", byte_to_binary(actual));
}
}
}
unsigned int setbits(unsigned int x, int p, int n, unsigned int y) {
unsigned int x_mask;
unsigned int y_mask;
/* mask out the x */
x_mask = (~( ~0 << p )) | (~0 << p+n);
/* mask out the y */
y_mask = ~x_mask;
return (x & x_mask) | (y & y_mask);
}
const char *byte_to_binary (int x) {
static char b[9];
b[0] = '\0';
int z;
for (z = 256; z > 0; z >>= 1) {
strcat(b, ((x & z) == z) ? "1" : "0");
}
return b;
}