Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- typedef void (*bit_fn_t)(const int, const int);
- // extract and transform bits
- void transform_bits(bit_fn_t bit_fn, unsigned int x, int n) {
- for (int idx = n - 1; idx >= 0; --idx) {
- const int bit = (x >> idx) & 1;
- bit_fn(idx, bit);
- }
- }
- // function to apply to each bit
- void bitFoo(const int bit_idx, const int bit) {
- printf("%d) %d\n", bit_idx, bit);
- }
- int main() {
- unsigned int x = 103; // = '01100111'
- int n = 8; // number of bits to extract
- printf("> bits of %u:\n", x);
- transform_bits(bitFoo, x, n);
- printf("\n");
- return 0;
- }
- //** compile:
- // gcc -std=c11 -Wall -O3 -o bits.exe bits.c
- //** run:
- // ./bits.exe
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement