• Tolookah@discuss.tchncs.de
    link
    fedilink
    arrow-up
    41
    ·
    1 year ago

    I use bit masks, suck it! (Really though, programming on an embedded CPU might be reasonable to do this, depending on the situation, but on a PC, trying to not waste bits wastes time)

    • Binette@lemmy.ml
      link
      fedilink
      arrow-up
      13
      ·
      1 year ago

      exactly! it is more costly for your pc cpu to check for a bit inside a byte, than just get the byte itself, because adresses only point to bytes

    • jsomae@lemmy.ml
      link
      fedilink
      arrow-up
      3
      ·
      1 year ago

      Unlikely. Most of the time on modern hardware, you’re going to be cache-limited, not cycle-limited. Checking one bit in a register is insanely fast.

  • ch00f
    link
    fedilink
    arrow-up
    30
    arrow-down
    1
    ·
    1 year ago

    I’ve been working on disassembling some 8-bit code from the 90s. Fuckers returned bits from functions using the overflow bit. Nuts.

    • CarrotsHaveEars@lemmy.ml
      link
      fedilink
      arrow-up
      6
      ·
      1 year ago

      What era was that device? Some old games on NES had to use all kinds of quirks like this to overcome hardware limitation.

      • ch00f
        link
        fedilink
        arrow-up
        2
        ·
        1 year ago

        It’s in an AlphaSmart. I’m working through disassembling the ROM to add some new features.

    • jsomae@lemmy.ml
      link
      fedilink
      arrow-up
      4
      ·
      1 year ago

      Where do you go to talk about such things? Could be fun to have a retro reversing community.

  • jsomae@lemmy.ml
    link
    fedilink
    arrow-up
    26
    ·
    edit-2
    1 year ago

    Use bit-fields:

    struct {
      bool a : 1;
      bool b : 1;
      bool c : 1;
      //...
    };
    

    Edit: careful not to use a 1-bit signed int, since the only values are 0 and -1, not 0 and 1. This tripped me up once.

      • kora@sh.itjust.works
        link
        fedilink
        arrow-up
        6
        ·
        edit-2
        1 year ago

        Yes, firmware running on bare metal requires good resource management. My current development board processor contains 512KB SRAM. That’s equivalent to half of the size of an average PDF.

      • jsomae@lemmy.ml
        link
        fedilink
        arrow-up
        3
        ·
        1 year ago

        Yes, because cache optimization is still important. Also useful to keep the size of packets down, to reduce the size of file formats, and anywhere that you use hundreds of thousands of instances of the struct.

        • Homosexual sapiens@lemmy.blahaj.zone
          link
          fedilink
          arrow-up
          1
          ·
          1 year ago

          For the packet size and fils format issues, it seems like this language feature would be less reliable than bit shifting or masking, given that different implementations may store the bits in a different order or not compactly

      • homura1650@lemm.ee
        link
        fedilink
        arrow-up
        1
        ·
        1 year ago

        I’ve used it a fair amount for memory mapped IO where the hardware defined bitfields. It is also useful when you have a data format with bitfields. I’d say it is also useful when your data does not respect byte boundaries, but the only time I’ve run into that involved the bit order being “backwards”, which means that I still had to bittwidle things back together.

        From a performance perspective, a cache line is only 64 bytes. Space in registers, low level memory caches, and memory throughout are all limited as well.

  • lorty@lemmy.ml
    link
    fedilink
    arrow-up
    16
    ·
    1 year ago

    If you want to optimize to this point, do some embedded development. It’s somewhat fun to work at such a low level (testing tends to be annoying though)