Skip to content

Instantly share code, notes, and snippets.

@theoknock
Last active February 1, 2022 17:11
Show Gist options
  • Save theoknock/daafdbd52884d982b095491d7c99c3b2 to your computer and use it in GitHub Desktop.
Save theoknock/daafdbd52884d982b095491d7c99c3b2 to your computer and use it in GitHub Desktop.
Predicated block invocation. Per Wikipedia (predication, computer science): “[I]nstead of using a conditional branch to select an instruction or a sequence of instructions to execute based on the predicate that controls whether the branch occurs, the instructions to be executed are associated with that predicate, so that they will be executed, o…
// Uses XOR to determine whether 'number' is even or odd and...
// ...executes the corresponding block
// Note: the catch is, in order for the expression to be evaluated, the blocks must return long. Any arbitrary value will do.
const long(^ const even_number)(void) = ^ long(void) {
printf("even_number\n");
return 2;
};
const long(^ const odd_number)(void) = ^ long(void) {
printf("odd_number\n");
return 1;
};
for (int number = 0; number < 10; number++) {
// ((active_component_bit_vector & MASK_ALL) && printf("filter\t%f\n", touch_angle)) || ((active_component_bit_vector & ~MASK_ALL) && printf("reduce\t%f\n", touch_angle));
((number % 2 ^ 1) & odd_number()) || even_number(); // WRONG
}
@theoknock
Copy link
Author

This answers the question: How do you apply bitwise operations (comparisons) to non-numerics, such as blocks?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment