• elucubra@sopuli.xyz
    link
    fedilink
    arrow-up
    4
    ·
    8 hours ago

    Could a kind soul ELI5 this? Well, maybe ELI8. I did quite a bit of programming in the 90-00s as part of my job, although nowadays I’m more of a script kiddie.

    • superheitmann@programming.dev
      link
      fedilink
      arrow-up
      7
      ·
      8 hours ago

      A Boolean is a true/false value. It can only be those two values and there be represented by a single bit (1 or 0).

      In most languages a Boolean variable occupies the space of a full byte (8 bit) even though only a single of those bits is needed for representing the Boolean.

      That’s mostly because computers can’t load a bit. They can only load bytes. Your memory is a single space where each byte has a numeric address. Starting from 0 and going to whatever amount of memory you have available. This is not really true because on most operating systems each process gets a virtual memory space but its true for many microcontrollers. You can load and address each f these bytes but it will always be a byte. That’s why booleans are stored as bytes because youd have to pack them with other data on the same address other wise and that’s getting complicated.

      Talking about getting complicated, in C++ a std::vector<bool> is specialized as a bit field. Each of the values in that vector only occupy a single bit and you can get a vector of size 8 in a single byte. This becomes problematic when you want to store references or pointers to one of the elements or when you’re working with them in a loop because the elements are not of type bool but some bool-reference type.

    • feddup@feddit.uk
      link
      fedilink
      English
      arrow-up
      3
      ·
      8 hours ago

      A boolean value only needs 1 bit (on or off) for true or false. However the smallest bit of addressable memory is a byte (8 bits) hence 7 are technically wasted.

      For low memory devices you could instead store 8 different Boolean values in one single byte by using bit masking instead