Skip to content

Commit 0b143a0

Browse files
committed
Fix #10; skipping final bit in flip.c
Reading all 31 bits is more efficient, but the scan order from low-to-high was giving problems in the test suite (since rand() is not accurate on the LSB portion). The scan order has thus been switched from LSB->MSB to MSB->LSB.
1 parent 4fb9c0b commit 0b143a0

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/c/flip.c

+18-6
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,27 @@
1111

1212
#include "flip.h"
1313

14-
static int k = 30;
14+
static int k = 31;
1515
static int flip_word = 0;
16-
static int flip_pos = 0;
16+
static int flip_pos = 31;
1717

1818
int flip(void){
19-
if (flip_pos == 0) {
19+
if (flip_pos == k) {
2020
flip_word = rand();
21-
flip_pos = k;
21+
flip_pos = 0;
2222
}
23-
--flip_pos;
24-
return (flip_word >> flip_pos) & 1;
23+
return (flip_word >> flip_pos++) & 1;
2524
}
25+
26+
// Old implementation scans from lower-to-higher order bits,
27+
// but rand() has poor quality on lower order bits.
28+
// int flip(void){
29+
// static int k = 31;
30+
// static int flip_word = 0;
31+
// static int flip_pos = 0;
32+
// if (flip_pos == 0) {
33+
// flip_word = rand();
34+
// flip_pos = k;
35+
// }
36+
// return (flip_word >> --flip_pos) & 1;
37+
// }

0 commit comments

Comments
 (0)