the-code-journal / website

0 stars 0 forks source link

Post - Identifying Odd/Even without Modulus Operator #32

Closed the-code-journal closed 3 years ago

the-code-journal commented 3 years ago

A lot of coding problems on various platforms ask about identifying if a number is odd or even. The programming languages provide a convenient module operator(%) which people usually use. However, modulus operator is expensive it involves division(successive subtraction) and it is not performant.

If the requirement is just to identify if a number is even or odd, bitwise operator AND can be used for this purpose.