this post was submitted on 15 May 2025
1158 points (98.6% liked)

Programmer Humor

23398 readers
1678 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] KindaABigDyl@programming.dev 183 points 1 week ago (3 children)
typedef 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;
[–] kiri@ani.social 44 points 1 week ago

You beat me to it!

[–] xthexder@l.sw0.com 41 points 1 week ago* (last edited 1 week ago) (1 children)

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 booleans

[–] Sonotsugipaa@lemmy.dbzer0.com 10 points 1 week ago* (last edited 1 week ago)

That's only for C++, as far as I can tell that struct is valid C

[–] h4x0r@lemmy.dbzer0.com 16 points 1 week ago (1 children)

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
  }
}
[–] anotherandrew@lemmy.mixdown.ca 1 points 6 days ago* (last edited 6 days ago)

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.