Determine if A Number is Even or Odd the Fastest Way

Category:
Dates and Math
Type:
Snippets
Difficulty:
Intermediate
Author:
Marcel Slooff

Version Compatibility: Visual Basic 6, Visual Basic 5

More information:
When writting code that has to be fast you need to consider CPU usage at a low level. Determining if a number is odd or even can be done in various ways. You could devide the number by 2 and see if there is a fraction remaining or you could use the MOD function. Either way you are performing an operation that is quite heavy for the processor (in comparison to a bitwise AND operation). This code is even faster.

Instructions: Copy the declarations and code below and paste directly into your VB project.

Declarations:

Code: