Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skipping final bit in flip.c #10

Open
fsaad opened this issue Feb 25, 2024 · 1 comment
Open

Skipping final bit in flip.c #10

fsaad opened this issue Feb 25, 2024 · 1 comment

Comments

@fsaad
Copy link
Collaborator

fsaad commented Feb 25, 2024

static int k = 30;
static int flip_word = 0;
static int flip_pos = 0;
int flip(void){
if (flip_pos == 0) {
flip_word = rand();
flip_pos = k;
}
--flip_pos;
return (flip_word >> flip_pos) & 1;
}

rand() returns a random 31-bit integer packed into a 32-bit integer (whose sign bit is always zero). The positions of the extracted bits should then be 0, ..., 30. However, by setting k=30, flip_pos = k, and flip_pos --, we only ever extract bits the bits at locations 0, ..., 29.

Also the code can be made more general to determine k automatically from limits.h and RAND_MAX.

@fsaad
Copy link
Collaborator Author

fsaad commented Feb 25, 2024

The lower order bits are poor quality in C, so when bumping k=31 we should change the scan order from MSB to LSB.

@fsaad fsaad closed this as completed in 0b143a0 Feb 25, 2024
fsaad added a commit that referenced this issue Feb 25, 2024
@fsaad fsaad reopened this Feb 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant