• gamer@lemm.ee
      link
      fedilink
      arrow-up
      5
      ·
      edit-2
      5 hours ago

      Consider what the disassembly would look like. There’s no fast way to do it.

      It’s also unnecessary since 8 bytes is a negligible amount in most cases. Serialization is the only real scenario where it matters. (Edit: and embedded)

    • Anders429@programming.dev
      link
      fedilink
      arrow-up
      25
      ·
      8 hours ago

      It would be slower to read the value if you had to also do bitwise operations to get the value.

      But you can also define your own bitfield types to store booleans packed together if you really need to. I would much rather that than have the compiler do it automatically for me.

    • timhh@programming.dev
      link
      fedilink
      arrow-up
      21
      ·
      9 hours ago

      Well there are containers that store booleans in single bits (e.g. std::vector<bool> - which was famously a big mistake).

      But in the general case you don’t want that because it would be slower.

  • acockworkorange@mander.xyz
    link
    fedilink
    arrow-up
    31
    ·
    12 hours 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
      15
      ·
      12 hours 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
        22
        ·
        12 hours 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

  • skisnow@lemmy.ca
    link
    fedilink
    English
    arrow-up
    45
    ·
    18 hours 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)
    
    
    • ethancedwards8@programming.dev
      link
      fedilink
      English
      arrow-up
      1
      ·
      24 minutes ago

      Okay. Gen z programmer here. Can you explain this black magic? I see it all the time in kernel code but I have no idea what it means.

    • Quatlicopatlix@feddit.org
      link
      fedilink
      arrow-up
      7
      ·
      9 hours 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.

  • visnae@lemmy.world
    link
    fedilink
    arrow-up
    5
    ·
    14 hours ago

    3GPP has an interesting way of serialising bools on the wire with ASN.1

    NULL OPTIONAL

    meaning only the type would be stored if true, otherwise it won’t be set at all

    • anton@lemmy.blahaj.zone
      link
      fedilink
      arrow-up
      1
      ·
      4 hours ago

      That requires some form of self describing format and will probably look like a sparse matrix in the end.

  • JakenVeina@lemm.ee
    link
    fedilink
    English
    arrow-up
    25
    arrow-down
    1
    ·
    21 hours ago

    It’s far more often stored in a word, so 32-64 bytes, depending on the target architecture. At least in most languages.

    • timhh@programming.dev
      link
      fedilink
      arrow-up
      4
      ·
      edit-2
      9 hours ago

      No it isn’t. All statically typed languages I know of use a byte. Which languages store it in an entire 32 bits? That would be unnecessarily wasteful.

  • glitchdx@lemmy.world
    link
    fedilink
    English
    arrow-up
    23
    arrow-down
    3
    ·
    21 hours ago

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

  • Subverb@lemmy.world
    link
    fedilink
    arrow-up
    37
    ·
    edit-2
    24 hours ago

    The 8-bit Intel 8051 family provides a dedicated bit-addressable memory space (addresses 20h-2Fh in internal RAM), giving 128 directly addressable bits. Used them for years. I’d imagine many microcontrollers have bit-width variables.

    bit myFlag = 0;

    Or even return from a function:

    bit isValidInput(unsigned char input) { // Returns true (1) if input is valid, false (0) otherwise return (input >= '0' && input <= '9'); }

      • Treczoks@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        8 hours ago

        Tell this to the LPC1114 I’m working with. Did you ever run a multilingual GUI from 2kbytes RAM on a 256x32 pixel display?

      • malank@lemm.ee
        link
        fedilink
        arrow-up
        10
        ·
        21 hours ago

        ARM has bit-banding specifically for this. I think it’s limited to M-profile CPUs (e.g. v7-M) but I’ve definitely used this before. It basically creates a 4-byte virtual address for every bit in a region. So the CPU itself can’t “address” a bit but it can access an address backed by only 1 bit of SRAM or registers (this is also useful to atomically access certain bits in registers without needing to use SW atomics).

    • the_tab_key@lemmy.world
      link
      fedilink
      arrow-up
      13
      ·
      23 hours ago

      We could go the other way as well: TI’s C2000 microcontroller architecture has no way to access a single byte, let alone a bit. A Boolean is stored in 16-bits on that one.

  • KindaABigDyl@programming.dev
    link
    fedilink
    arrow-up
    166
    ·
    1 day 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
      13
      ·
      20 hours 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
        8 hours 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
      36
      ·
      edit-2
      1 day 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

  • Amberskin@europe.pub
    link
    fedilink
    arrow-up
    4
    ·
    16 hours ago

    Pl/1 did it right:

    Dcl 1 mybools, 3 bool1 bit(1) unaligned, 3 bool2 bit(1) unaligned, … 3 bool8 bit(1) unaligned;

    All eight bools are in the same byte.