coding chess - eviltoast
  • ApexHunter@lemmy.ml
    link
    fedilink
    arrow-up
    9
    ·
    edit-2
    9 months ago

    Is this meant to be a joke or is it intended to be a serious solution?

    Asking for someone who lacks a sense of humor.

    Ok, fine, I’m asking for me. That person is me.

    • Buddahriffic@lemmy.world
      link
      fedilink
      arrow-up
      12
      ·
      9 months ago

      I want to see how you’d reply if I said it was serious. So let’s go with that. This is the best way to determine if a number is even in c/c++.

      • ApexHunter@lemmy.ml
        link
        fedilink
        arrow-up
        6
        ·
        edit-2
        9 months ago

        The reply would have been return x % 2 == 0, or if you wanted it to be less readable return !(x&1).

        But if you were going for a way that is subtly awful or expensive, just do a regex match on “[02468]$”. You don’t get a stack overflow with larger numbers but I struggle to think of a plausible bit of code that consumes more unnnecessary cycles than that…

        • Buddahriffic@lemmy.world
          link
          fedilink
          arrow-up
          3
          ·
          9 months ago

          The less readable one is faster, though I’d be surprised if the compiler doesn’t generate the same code for both of those options with optimizations enabled.

          I like the regex one as another unnecessarily complicated way of doing it. It also involves a string conversion.

          The way that was being hinted at before my reply was a large series of if statements: is it 2? 4? 6? 8? And so on. It’s plausible in that I could see a beginner programmer using that method. And honestly, knowing how people can get pigeon holed into looking at a problem from some weird angle but still having the determination to figure it out, most solutions are plausible. Often when they get pointed in a better direction, it’s not so much a case of them learning something new as it is a facepalm kinda moment where they feel embarrassed at not seeing that. I’ve done it myself many times lol.

          It’s always funny seeing someone use the wrong tool for the job but still getting the job done, even when it’s me.

            • Buddahriffic@lemmy.world
              link
              fedilink
              arrow-up
              1
              ·
              9 months ago

              Awesome, thank you for that link. I should have guessed this was a thing but it hadn’t even occurred to me.

              And yeah, it is surprising that clang doesn’t treat mod power of 2 as a special case. It looks like I can’t type in code on mobile, does clang handle divide by power of 2 as a idiv or shift?