I saw one example

int x = 10;
int y = 5;

bool isGreater = x > y;

printf("%d", isGreater);

But I could write this

int x = 10;
int y = 5;

printf("%d", x > y);

I am a complete beginner and I have no real reason why I would or would not want to deal with boolean variables, but I want to understand their raison d’être.

Edit: typo city

  • OwOarchist@pawb.social
    link
    fedilink
    English
    arrow-up
    9
    ·
    1 day ago

    First, if you do it with an int, you’re probably using up 32bits per value.

    One project I worked with stored boolean values as bits in an 8-bit int, and then used binary math to read and write individual bits from that int, with each bit representing a distinct and independent boolean variable.

    Really weird, complicated, and sounds kind of dumb … but it worked, and it was extremely memory-efficient.

    • Rossphorus@lemmy.world
      link
      fedilink
      English
      arrow-up
      4
      ·
      edit-2
      5 hours ago

      C++ actually does this ‘optimisation’ for std::vector<bool>. I say ‘optimisation’ because it effectively trades time for space, which is usually the opposite of what you want - space is cheap, time usually isn’t. Sometimes it’s a good tradeoff though - it’s common in embedded development where you might only have a few kB of RAM.