/* example of using GCC anonymous structures and unions to do quick bitfields */ #include #include struct foo { union { struct { unsigned x:1; unsigned y:1; unsigned z:1; }; unsigned long n; /* TODO: a sizeof of the struct could make an array */ }; }; void dump_foo(struct foo *foo) { printf("x:%d y:%d z:%d\n", foo->x, foo->y, foo->z); } int main() { struct foo mask, foo; foo.n=0; /* initialize */ /** create some set **/ foo.x=0; foo.y=1; foo.z=1; printf("original: "); dump_foo(&foo); /** make a mask **/ mask.n=0; /* initialize */ mask.x=1; mask.y=0; mask.z=1; printf("mask : "); dump_foo(&mask); /** apply the mask **/ foo.n&=mask.n; printf("final : "); dump_foo(&foo); return 0; }