• mellejwz@lemmy.world
    link
    fedilink
    English
    arrow-up
    4
    arrow-down
    1
    ·
    8 months ago

    Not neccessarily wrong, but you could also check the first bit. If it’s 1 the number is uneven, if it’s 0 the number is even. That seems to be more efficient.

    • dukk@programming.dev
      link
      fedilink
      arrow-up
      5
      ·
      8 months ago

      That’s what I was thinking too… Although, I wouldn’t be surprised if most languages convert modulo 2 to this automatically.

        • dukk@programming.dev
          link
          fedilink
          arrow-up
          2
          ·
          8 months ago

          That’s the main issue with premature optimization: do it the “optimized” way and it may still be inefficient, or do it the obvious way and let the compiler turn it into its most optimized form. (Of course, not the case with all languages, but most mainstream compilers optimize the code to a decent extent.)

    • herrvogel@lemmy.world
      link
      fedilink
      arrow-up
      3
      ·
      8 months ago

      Modern compilers and interpreters are smart enough to figure out what you’re trying to do and automatically do that for you.

    • ziviz@lemmy.sdf.org
      link
      fedilink
      English
      arrow-up
      2
      ·
      8 months ago

      Huh… That makes sense. Til. Ran some tests but speed is pretty similar. Only 4% faster using bitmath or 300 milliseconds difference after 10mil runs.