Skip to content

Skipping final bit in flip.c #10

@fsaad

Description

@fsaad

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions