• acockworkorange@mander.xyz
    link
    fedilink
    arrow-up
    1
    ·
    1 year ago

    In the industrial automation world and most of the IT industry, data is aligned to the nearest word. Depending on architecture, that’s usually either 16, 32, or 64 bits. And that’s the space a single Boolean takes.

  • Lucy :3@feddit.org
    link
    fedilink
    arrow-up
    1
    ·
    1 year ago

    Then you need to ask yourself: Performance or memory efficiency? Is it worth the extra cycles and instructions to put 8 bools in one byte and & 0x bitmask the relevant one?

  • KindaABigDyl@programming.dev
    link
    fedilink
    arrow-up
    1
    ·
    1 year ago
    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;
    
  • skisnow@lemmy.ca
    link
    fedilink
    English
    arrow-up
    0
    ·
    1 year ago

    Back in the day when it mattered, we did it like

    #define BV00		(1 <<  0)
    #define BV01		(1 <<  1)
    #define BV02		(1 <<  2)
    #define BV03		(1 <<  3)
    ...etc
    
    #define IS_SET(flag, bit)	((flag) & (bit))
    #define SET_BIT(var, bit)	((var) |= (bit))
    #define REMOVE_BIT(var, bit)	((var) &= ~(bit))
    #define TOGGLE_BIT(var, bit)	((var) ^= (bit))
    
    ....then...
    #define MY_FIRST_BOOLEAN BV00
    SET_BIT(myFlags, MY_FIRST_BOOLEAN)
    
    
    • Quatlicopatlix@feddit.org
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago

      With embedded stuff its still done like that. And if you go from the arduino functionss to writing the registers directly its a hell of a lot faster.

  • Skullgrid@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    1 year ago

    just like electronic components, they sell the gates by the chip with multiple gates in them because it’s cheaper

  • kiri@ani.social
    link
    fedilink
    arrow-up
    0
    ·
    1 year ago

    I have a solution with a bit fields. Now your bool is 1 byte :

    struct Flags {
        bool flag0 : 1;
        bool flag1 : 1;
        bool flag2 : 1;
        bool flag3 : 1;
        bool flag4 : 1;
        bool flag5 : 1;
        bool flag6 : 1;
        bool flag7 : 1;
    };
    

    Or for example:

    struct Flags {
        bool flag0 : 1;
        bool flag1 : 1:
        int x_cord : 3;
        int y_cord : 3;
    };