cm0002@lemmy.world to Programmer Humor@programming.dev · 2 days agoTell me the truth ...piefed.jeena.netimagemessage-square132fedilinkarrow-up11.13Karrow-down116
arrow-up11.11Karrow-down1imageTell me the truth ...piefed.jeena.netcm0002@lemmy.world to Programmer Humor@programming.dev · 2 days agomessage-square132fedilink
minus-squareh4x0r@lemmy.dbzer0.comlinkfedilinkEnglisharrow-up15·2 days 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 } }
minus-squareanotherandrew@lemmy.mixdown.calinkfedilinkEnglisharrow-up1·edit-21 day agoWhy not if (f & (F_1 | F_3)) {? I use this all the time in embedded code. edit: never mind; you’re checking for both flags. I’d probably use (f & (F_1 | F_3)) == (F_1 | F_3) but that’s not much different than what you wrote.
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 } }
Why not
if (f & (F_1 | F_3)) {
? I use this all the time in embedded code.edit: never mind; you’re checking for both flags. I’d probably use
(f & (F_1 | F_3)) == (F_1 | F_3)
but that’s not much different than what you wrote.