java - Is binary operation more efficient than modulo? -


there 2 ways check if number divisible 2:

  • x % 2 == 1
  • (x & 1) == 1

which of 2 more efficient?

the bit operation faster.

division/modulus generalized operation must work divisor provide, not 2. must check underflow, range errors , division zero, , maintain remainder, of takes time.

the bit operation bit "and" operation, in case happens correspond division two. might use single processor operation execute.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -