Does anyone know a previous implementation?
#include <stdio.h> #define bitoffsetof(t, f) \ ({ union { unsigned long long raw; t typ; }; \ raw = 0; ++typ.f; __builtin_ctzll(raw); })
#define bitsizeof(t, f) \ ({ union { unsigned long long raw; t typ; }; \ raw = 0; --typ.f; 8*sizeof(raw)-__builtin_clzll(raw)\ -__builtin_ctzll(raw); })
struct RGB565 { unsigned short r:5, g:6, b:5; }; int main() { printf("offset(width): r=%d(%d) g=%d(%d) b=%d(%d)\n", bitoffsetof(RGB565, r), bitsizeof(RGB565, r), bitoffsetof(RGB565, g), bitsizeof(RGB565, g), bitoffsetof(RGB565, b), bitsizeof(RGB565, b)); }
//$ gcc bitfieldtest.cpp && ./a.out //offset(width): r=0(5) g=5(6) b=11(5) //
[1] https://twitter.com/suarezvictor/status/1477697986243272706