Sort if you use bit fields. However, keep in mind that bit fields are still packed inside some built-in type. In the example below, both has_foo and foo_count are "packed" inside an unsigned integer that uses four bytes on my machine.
#include <stdio.h> struct data { unsigned int has_foo : 1; unsigned int foo_count : 7; }; int main(int argc, char* argv[]) { data d; d.has_foo = 1; d.foo_count = 42; printf("d.has_foo = %u\n", d.has_foo); printf("d.foo_count = %d\n", d.foo_count); printf("sizeof(d) = %lu\n", sizeof(d)); return 0; }
source share