-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbit-exercises.c
133 lines (108 loc) · 3.07 KB
/
bit-exercises.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <assert.h>
#include "dump-bits.h"
unsigned get_clear_mask(int p, int n)
{
return (~(~0 << n)) << p;
}
/*
Exercise 2-6. Write a function setbits(x,p,n.y) that returns x ith the n
bits that begin at position p set to the rightmost n bits of y, leaving
the other bits unchanged.
*/
unsigned setbits(unsigned x, int p, int n, unsigned y)
{
unsigned y_mask;
y_mask = (y & get_clear_mask(0, n)) << (p - n + 1);
// first set our target section of x to all zeros,
// then 'or' with our mask
return (x & ~get_clear_mask(p - n + 1, n)) | y_mask;
}
void test_setbits(unsigned x, int y, int p, int n, unsigned expected_result)
{
unsigned result = setbits(x, p, n, y);
printf("\nResults:\n\n");
printf("\t%-15s", "Expected:");
dump_bits(expected_result);
printf("\t%-15s", "Actual:");
dump_bits(result);
assert(result == expected_result);
}
/*
Exercise 2-7. Write a function invert(x,p,n) that returns x with the n bits
that begin at position p inverted (i.e., 1 changed into 0 and vice versa), leaving
the others untouched.
*/
unsigned invert(unsigned x, int p, int n)
{
unsigned inverted_mask = (~x & get_clear_mask(p - n + 1, n));
return (x & ~get_clear_mask(p - n + 1, n)) | inverted_mask;
}
void test_invert(unsigned x, int p, int n, unsigned expected_result)
{
unsigned result = invert(x, p, n);
printf("\nResults:\n\n");
printf("\t%-15s", "Expected:");
dump_bits(expected_result);
printf("\t%-15s", "Actual:");
dump_bits(result);
assert(result == expected_result);
}
/*
Exercise 2-8. Write a function rightrot(x,n) that returns the value of the integer rotated to the right by n bit positions.
*/
//hacky...
int count_bits()
{
int x = 1;
int count = 1;
while(x > 0) {
x <<= 1;
count++;
}
return count;
}
int rightrot(int x, int n)
{
static int number_of_bits_to_carry = 0;
int carry;
unsigned result = x;
if (number_of_bits_to_carry == 0) {
number_of_bits_to_carry = count_bits() - 1;
}
for(int i = 0; i < n; i++) {
carry = (result & 1) << number_of_bits_to_carry;
result = (result >> 1) | carry;
}
return result;
}
void test_rightrot(int x, int n, int expected_result)
{
int result = rightrot(x, n);
printf("\nResults:\n\n");
printf("\t%-15s", "Expected:");
dump_bits(expected_result);
printf("\t%-15s", "Actual:");
dump_bits(result);
printf("\n");
assert(result == expected_result);
}
int main()
{
/* printf("%ld\n", strtol("11100000000000000000000000000001", NULL, 2)); */
/* test_setbits(87, 458, 7, 4, 167); */
/* test_setbits(144, 7, 5, 3, 184); */
/* test_invert(170, 5, 4, 150); */
/* test_invert(65280, 13, 2, 52992); */
test_rightrot(strtol("00000000000000000000000000001111", NULL, 2),
3,
strtol("11100000000000000000000000000001", NULL, 2));
test_rightrot(strtol("01000000000000000000000000001010", NULL, 2),
5,
strtol("01010010000000000000000000000000", NULL, 2));
test_rightrot(strtol("00000000000000000000000010000000", NULL, 2),
1,
strtol("00000000000000000000000001000000", NULL, 2));
}