You are not logged in. Please login at to post your questions!


bitwise & and modulus %2 operator speed comparison


i dont remember the source but i had read somewhere that bitwise & of n with 1 to check if its even or odd is faster than checking n%2 . but while solving the problem traversing the grid, i checked if n is odd or even and same for m. in that i found the version of code which had % operator ran in 0.00 and the one with bitwise & ran in 0.03 and 0.02 (i ran this code two times) rest all code was same. per test case i am checking only one time the two numbers about even and odd. Can someone tell which is faster and if possible, give reasons cheers

asked 04 Jul '15, 03:20

ankursmooth's gravatar image

accept rate: 14%

Such a small difference in performance is because of the SPOJ server. Try submitting the same solution twice and you may see a difference of 0.01-0.02 due to the load on the server. Plus, majority of the execution time of the code is because of input/looping, therefore using '&' operator though faster, is unable to provide a concrete speed up in this case.


answered 04 Jul '15, 08:20

anonxyz596's gravatar image

accept rate: 0%

edited 04 Jul '15, 08:21

toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 04 Jul '15, 03:20

question was seen: 1,669 times

last updated: 04 Jul '15, 08:21