• Treczoks@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    2 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.

  • steeznson@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    2 months 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).

    • mmddmm@lemm.ee
      link
      fedilink
      arrow-up
      0
      ·
      2 months 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
        arrow-up
        0
        ·
        2 months 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?

        • brian@programming.dev
          link
          fedilink
          arrow-up
          0
          ·
          2 months ago

          things that store it as word size for alignment purposes (most common afaik), things that pack multiple books into one byte (normally only things like bool sequences/structs), etc

          • timhh@programming.dev
            link
            fedilink
            arrow-up
            0
            ·
            2 months ago

            things that store it as word size for alignment purposes

            Nope. bools only need to be naturally aligned, so 1 byte.

            If you do

            struct SomeBools {
              bool a;
              bool b;
              bool c;
              bool d;
            };
            

            its 4 bytes.

            • brian@programming.dev
              link
              fedilink
              arrow-up
              0
              ·
              2 months ago

              sure, but if you have a single bool in a stack frame it’s probably going to be more than a byte. on the heap definitely more than a byte

              • timhh@programming.dev
                link
                fedilink
                arrow-up
                0
                ·
                2 months ago

                but if you have a single bool in a stack frame it’s probably going to be more than a byte.

                Nope. - if you can’t read RISC-V assembly, look at these lines

                        sb      a5,-17(s0)
                ...
                        sb      a5,-18(s0)
                ...
                        sb      a5,-19(s0)
                ...
                

                That is it storing the bools in single bytes. Also I only used RISC-V because I’m way more familiar with it than x86, but it will do the same thing.

                on the heap definitely more than a byte

                Nope, you can happily malloc(1) and store a bool in it, or malloc(4) and store 4 bools in it. A bool is 1 byte. Consider this a TIL moment.

                • brian@programming.dev
                  link
                  fedilink
                  arrow-up
                  0
                  ·
                  2 months ago

                  c++ guarantees that calls to malloc are aligned https://en.cppreference.com/w/cpp/memory/c/malloc .

                  you can call malloc(1) ofc, but calling malloc_usable_size(malloc(1)) is giving me 24, so it at least allocated 24 bytes for my 1, plus any tracking overhead

                  yeah, as I said, in a stack frame. not surprised a compiler packed them into single bytes in the same frame (but I wouldn’t be that surprised the other way either), but the system v abi guarantees at least 4 byte alignment of a stack frame on entering a fn, so if you stored a single bool it’ll get 3+ extra bytes added on the next fn call.

                  computers align things. you normally don’t have to think about it. Consider this a TIL moment.

        • mmddmm@lemm.ee
          link
          fedilink
          arrow-up
          0
          ·
          2 months ago

          Apart from dynamically typed languages which need to store the type with the value

          You know that depending on what your code does, the same C that people are talking upthread doesn’t even need to allocate memory to store a variable, right?

            • timhh@programming.dev
              link
              fedilink
              arrow-up
              0
              ·
              2 months ago

              I think he’s talking about if a variable only exists in registers. In which case it is the size of a register. But that’s true of everything that gets put in registers. You wouldn’t say uint16_t is word-sized because at some point it gets put into a word-sized register. That’s dumb.

      • anton@lemmy.blahaj.zone
        link
        fedilink
        arrow-up
        0
        ·
        2 months ago

        C/C++ considers an nonzero number, as your true value but false is only zero. This would allow you to guard against going from true to false via bit flip but not false to true.
        Other languages like rust define 0 to be false and 1 to be true and any other bit pattern to be invalid for bools.

  • finitebanjo@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    2 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?

  • SW42@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    2 months ago

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

  • kiri@ani.social
    link
    fedilink
    arrow-up
    0
    ·
    2 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
      0
      ·
      2 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.

  • KindaABigDyl@programming.dev
    link
    fedilink
    arrow-up
    0
    ·
    2 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;
    
    • xthexder@l.sw0.com
      link
      fedilink
      arrow-up
      0
      ·
      edit-2
      2 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

    • h4x0r@lemmy.dbzer0.com
      link
      fedilink
      English
      arrow-up
      0
      ·
      2 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
        }
      }
      
  • ssfckdt@lemmy.blahaj.zone
    link
    fedilink
    arrow-up
    0
    ·
    2 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

    • excral@feddit.org
      link
      fedilink
      arrow-up
      0
      ·
      2 months ago

      In terms of memory usage it’s a waste. But in terms of performance you’re absolutely correct. It’s generally far more efficient to check is a word is 0 than to check if a single bit is zero.

    • Aux@feddit.uk
      link
      fedilink
      English
      arrow-up
      0
      ·
      2 months ago

      Usually the most effective way is to read and write the same amount of bits as the architecture of the CPU, so for 64 bit CPUs it’s 64 bits at once.

    • Gsus4@mander.xyz
      link
      fedilink
      arrow-up
      0
      ·
      edit-2
      2 months ago

      Weird how I usually learn more from the humor communities than the serious ones… 😎

  • mavu@discuss.tchncs.de
    link
    fedilink
    arrow-up
    0
    ·
    2 months ago

    This reminds me that I actually once made a class to store bools packed in uint8 array to save bytes.

    Had forgotten that. I think i have to update the list of top 10 dumbest things i ever did.