forked from hcs0/Hackers-Delight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flp2.c.txt
32 lines (28 loc) · 768 Bytes
/
flp2.c.txt
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
#include <stdio.h>
/* Round down to a power of 2. */
unsigned flp2(unsigned x) {
x = x | (x >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
x = x | (x >> 8);
x = x | (x >>16);
return x - (x >> 1);
}
int errors;
void main() {
int i;
static unsigned test[] = {0,0, 1,1, 2,2, 3,2, 4,4, 5,4, 7,4, 8,8,
9,8, 15,8, 16,16, 0xffff,0x8000, 0x7fffffff,0x40000000,
0x80000000,0x80000000, 0x80000001,0x80000000,
0xffffffff,0x80000000};
void error(int x);
for (i = 0; i < sizeof(test)/4; i += 2) {
if (flp2(test[i]) != test[i+1]) error(test[i]);
}
if (errors == 0)
printf("Passed all %d cases.\n", sizeof(test)/8);
}
void error(int x) {
errors = errors + 1;
printf("Error for x = %d, got %d\n", x, flp2(x));
}