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

c# - HttpResponseMessage System.InvalidOperationException -

sql - Postgresql error: "failed to find conversion function from unknown to text" -