-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.c
45 lines (41 loc) · 844 Bytes
/
utilities.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
#include "utilities.h"
void binprintf(int v) {
unsigned int mask = 1U << 31;
while(mask) {
printf("%d", (v&mask ? 1 : 0));
mask >>= 1;
}
printf("\n");
}
// calculates position of a highest '1'
unsigned pos(unsigned num) {
unsigned mask = 1U;
unsigned pos = 0;
for (int i = 0; i < 32; i++) {
if(mask & num) {
pos = i;
}
mask <<= 1;
}
return pos;
}
int power_of_two(unsigned num) {
unsigned mask = 1U << 31;
for (int i = 31; i >= 0; i--) {
if (num & mask) {
return i;
}
mask >>= 1;
}
return -1;
}
unsigned nearestPowerOfTwo(unsigned int v) { // return nearest power of two of v
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}