• KindaABigDyl@programming.dev
    link
    fedilink
    arrow-up
    74
    ·
    7 days 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;
    
  • Subverb@lemmy.world
    link
    fedilink
    arrow-up
    19
    ·
    edit-2
    6 days 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'); }

  • glitchdx@lemmy.world
    link
    fedilink
    English
    arrow-up
    11
    ·
    6 days ago

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

  • JakenVeina@lemm.ee
    link
    fedilink
    English
    arrow-up
    10
    ·
    6 days 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
      2
      ·
      edit-2
      6 days 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.

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

        C, C++, C#, to name the main ones. And quite a lot of languages are compiled similarly to these.

        To be clear, there’s a lot of caveats to the statement, and it depends on architecture as well, but at the end of the day, it’s rare for a byte or bool to be mapped directly to a single byte in memory.

        Say, for example, you have this function…

        public void Foo()
        {
            bool someFlag = false;
            int counter = 0;
        
            ...
        }
        

        The someFlag and counter variables are getting allocated on the stack, and (depending on architecture) that probably means each one is aligned to a 32-bit or 64-bit word boundary, since many CPUs require that for whole-word load and store instructions, or only support a stack pointer that increments in whole words. If the function were to have multiple byte or bool variables allocated, it might be able to pack them together, if the CPU supports single-byte load and store instructions, but the next int variable that follows might still need some padding space in front of it, so that it aligns on a word boundary.

        A very similar concept applies to most struct and object implementations. A single byte or bool field within a struct or object will likely result in a whole word being allocated, so that other variables and be word-aligned, or so that the whole object meets some optimal word-aligned size. But if you have multiple less-than-a-word fields, they can be packed together. C# does this, for sure, and has some mechanisms by which you can customize field packing.

    • borokov@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      5 days 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.

  • steeznson@lemmy.world
    link
    fedilink
    arrow-up
    4
    ·
    5 days ago

    We need to be able to express 0 and 1 as integers so that functionality is just being overloaded to express another concept.

    Wait until the person who made this meme finds out about how many bits are being wasted on modern CPU architectures. 7 is the minimum possible wasted bits but it would be 31 on every modern computer (even 64b machines since they default to 32b ints).

  • SW42@lemmy.world
    link
    fedilink
    arrow-up
    6
    ·
    7 days ago

    Joke’s on you, I always use 64 bit wide unsigned integers to store a 1 and compare to check for value.

  • Skullgrid@lemmy.world
    link
    fedilink
    arrow-up
    4
    ·
    7 days ago

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