• Tekhne
    link
    fedilink
    349 minutes ago

    Are you telling me that no compiler optimizes this? Why?

    • @timhh@programming.dev
      link
      fedilink
      420 minutes 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
    163 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
      93 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
        103 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

  • @visnae@lemmy.world
    link
    fedilink
    45 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

  • @skisnow@lemmy.ca
    link
    fedilink
    English
    319 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)
    
    
  • @Amberskin@europe.pub
    link
    fedilink
    37 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.

  • @JakenVeina@lemm.ee
    link
    fedilink
    English
    2012 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
      1
      edit-2
      17 minutes 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
    1712 hours ago

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

  • Subverb
    link
    fedilink
    32
    edit-2
    15 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'); }

    • @the_tab_key@lemmy.world
      link
      fedilink
      1114 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.

      • @malank@lemm.ee
        link
        fedilink
        712 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).

  • @KindaABigDyl@programming.dev
    link
    fedilink
    15920 hours 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
      1111 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
        }
      }
      
    • @xthexder@l.sw0.com
      link
      fedilink
      32
      edit-2
      18 hours 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

      • Sonotsugipaa
        link
        fedilink
        English
        6
        edit-2
        18 hours ago

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

    • @mmddmm@lemm.ee
      link
      fedilink
      13420 hours ago

      And compiler. And hardware architecture. And optimization flags.

      As usual, it’s some developer that knows little enough to think the walls they see around enclose the entire world.

      • @timhh@programming.dev
        link
        fedilink
        115 minutes ago

        I don’t think so. Apart from dynamically typed languages which need to store the type with the value, it’s always 1 byte, and that doesn’t depend on architecture (excluding ancient or exotic architectures) or optimisation flags.

        Which language/architecture/flags would not store a bool in 1 byte?

      • Lucien [he/him]
        link
        fedilink
        913 hours ago

        Fucking lol at the downvoters haha that second sentence must have rubbed them the wrong way for being too accurate.