cm0002@lemmy.world to Programmer Humor@programming.dev · 2 months agoTell me the truth ...piefed.jeena.netimagemessage-square152fedilinkarrow-up11arrow-down10
arrow-up11arrow-down1imageTell me the truth ...piefed.jeena.netcm0002@lemmy.world to Programmer Humor@programming.dev · 2 months agomessage-square152fedilink
minus-squareKindaABigDyl@programming.devlinkfedilinkarrow-up0·2 months agotypedef struct { bool a: 1; bool b: 1; bool c: 1; bool d: 1; bool e: 1; bool f: 1; bool g: 1; bool h: 1; } __attribute__((__packed__)) not_if_you_have_enough_booleans_t;
minus-squarexthexder@l.sw0.comlinkfedilinkarrow-up0·edit-22 months agoOr just std::bitset<8> for C++. Bit fields are neat though, it can store weird stuff like a 3 bit integer, packed next to booleans
minus-squareSonotsugipaa@lemmy.dbzer0.comlinkfedilinkEnglisharrow-up0·edit-22 months agoThat’s only for C++, as far as I can tell that struct is valid C
minus-squareh4x0r@lemmy.dbzer0.comlinkfedilinkEnglisharrow-up0·2 months agoThis was gonna be my response to OP so I’ll offer an alternative approach instead: typedef enum flags_e : unsigned char { F_1 = (1 << 0), F_2 = (1 << 1), F_3 = (1 << 2), F_4 = (1 << 3), F_5 = (1 << 4), F_6 = (1 << 5), F_7 = (1 << 6), F_8 = (1 << 7), } Flags; int main(void) { Flags f = F_1 | F_3 | F_5; if (f & F_1 && f & F_3) { // do F_1 and F_3 stuff } }
You beat me to it!
Or just
std::bitset<8>
for C++. Bit fields are neat though, it can store weird stuff like a 3 bit integer, packed next to booleansThat’s only for C++, as far as I can tell that struct is valid C
This was gonna be my response to OP so I’ll offer an alternative approach instead:
typedef enum flags_e : unsigned char { F_1 = (1 << 0), F_2 = (1 << 1), F_3 = (1 << 2), F_4 = (1 << 3), F_5 = (1 << 4), F_6 = (1 << 5), F_7 = (1 << 6), F_8 = (1 << 7), } Flags; int main(void) { Flags f = F_1 | F_3 | F_5; if (f & F_1 && f & F_3) { // do F_1 and F_3 stuff } }