• kiri@ani.social
    link
    fedilink
    arrow-up
    20
    ·
    edit-2
    3 months 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;
    };
    
    • lapping6596@lemmy.world
      link
      fedilink
      arrow-up
      3
      ·
      3 months ago

      I watched a YouTube video where a dev was optimizing unity code to match the size of data that is sent to the cpu using structs just like this.

    • borokov@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      3 months ago
      auto v = std::vector<bool>(8);
      bool* vPtr = v.data;
      vPtr[2] = true;
      // KABOOM !!!
      

      I’ve spent days tracking this bug… That’s how I learned about bool specialisation of std::vector.

  • finitebanjo@lemmy.world
    link
    fedilink
    arrow-up
    2
    ·
    3 months ago

    Does anybody ever figure in parity when comparing bit sizes and all that jazz or are we only ever concerned with storage space?

  • Treczoks@lemmy.world
    link
    fedilink
    arrow-up
    5
    ·
    3 months ago

    This guy never coded in KEIL C on an 8051 architecture. They actually use bit addressable RAM for booleans. And if you set the compiler to pass function parameters in registers, it uses the carry flag for the first bit or bool type parameter.

  • KindaABigDyl@programming.dev
    link
    fedilink
    arrow-up
    175
    ·
    3 months 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;
    
    • h4x0r@lemmy.dbzer0.com
      link
      fedilink
      English
      arrow-up
      15
      ·
      3 months ago

      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
        link
        fedilink
        English
        arrow-up
        1
        ·
        edit-2
        3 months 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.

    • xthexder@l.sw0.com
      link
      fedilink
      arrow-up
      37
      ·
      edit-2
      3 months ago

      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

  • Skullgrid@lemmy.world
    link
    fedilink
    arrow-up
    15
    ·
    3 months ago

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

  • elucubra@sopuli.xyz
    link
    fedilink
    arrow-up
    3
    ·
    3 months ago

    Could a kind soul ELI5 this? Well, maybe ELI8. I did quite a bit of programming in the 90-00s as part of my job, although nowadays I’m more of a script kiddie.

    • superheitmann@programming.dev
      link
      fedilink
      arrow-up
      9
      ·
      3 months ago

      A Boolean is a true/false value. It can only be those two values and there be represented by a single bit (1 or 0).

      In most languages a Boolean variable occupies the space of a full byte (8 bit) even though only a single of those bits is needed for representing the Boolean.

      That’s mostly because computers can’t load a bit. They can only load bytes. Your memory is a single space where each byte has a numeric address. Starting from 0 and going to whatever amount of memory you have available. This is not really true because on most operating systems each process gets a virtual memory space but its true for many microcontrollers. You can load and address each f these bytes but it will always be a byte. That’s why booleans are stored as bytes because youd have to pack them with other data on the same address other wise and that’s getting complicated.

      Talking about getting complicated, in C++ a std::vector<bool> is specialized as a bit field. Each of the values in that vector only occupy a single bit and you can get a vector of size 8 in a single byte. This becomes problematic when you want to store references or pointers to one of the elements or when you’re working with them in a loop because the elements are not of type bool but some bool-reference type.

      • Aux@feddit.uk
        link
        fedilink
        English
        arrow-up
        3
        arrow-down
        1
        ·
        3 months ago

        And performance optimisation of a compiler for a 64 bit CPU will realign everything and each boolean will occupy 8 bytes instead.

    • feddup@feddit.uk
      link
      fedilink
      English
      arrow-up
      4
      ·
      3 months ago

      A boolean value only needs 1 bit (on or off) for true or false. However the smallest bit of addressable memory is a byte (8 bits) hence 7 are technically wasted.

      For low memory devices you could instead store 8 different Boolean values in one single byte by using bit masking instead

  • acockworkorange@mander.xyz
    link
    fedilink
    arrow-up
    33
    ·
    3 months 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.

    • ZILtoid1991@lemmy.world
      link
      fedilink
      arrow-up
      16
      ·
      3 months ago

      That’s why I primarily use booleans in return parameters, beyond that I’ll try to use bitfields. My game engine’s tilemap format uses a 32 bit struct, with 16 bit selecting the tile, 12 bit selecting the palette, and 4 bit used for various bitflags (horizontal and vertical mirroring, X-Y axis invert, and priority bit).

      • acockworkorange@mander.xyz
        link
        fedilink
        arrow-up
        27
        ·
        3 months ago

        Bit fields are a necessity in low level networking too.

        They’re incredibly useful, I wish more people made use of them.

        I remember I interned at a startup programming microcontrollers once and created a few bitfields to deal with something. Then the lead engineer went ahead and changed them to masked ints. Because. The most aggravating thing is that an int size isn’t consistent across platforms, so if they were ever to change platforms to a different word length, they’d be fucked as their code was full of platform specific shenanigans like that.

        /rant

  • glitchdx@lemmy.world
    link
    fedilink
    English
    arrow-up
    25
    arrow-down
    3
    ·
    3 months ago

    if wasting a byte or seven matters to you, then then you need to be working in a lower level language.

  • ssfckdt@lemmy.blahaj.zone
    link
    fedilink
    arrow-up
    5
    ·
    3 months ago

    I swore I read that mysql dbs will store multiple bools in a row as bit maps in one byte. I can’t prove it though