you are viewing a single comment's thread
view the rest of the comments
[–] 3 points 2 years ago (3 children)

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.

  • source
  • parent
  • hideshow 6 child comments
  • [–] 5 points 2 years ago (1 child)

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

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 2 years ago (1 child)
  • [–] 2 points 2 years 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.)

  • source
  • parent