Determine if number is even

  • Is there a clean way to determine if a number is even?


    I can think of some really easy ways that don't invlove 100 ORs in my logic, but they all use modulus.


    Any really simple ways?

  • One option would be to setup a group output and set it equal to the number you want to check. Then just check the status of the first output in the group. If it is off then the number is even if it is on then the number is odd.

  • I actually have a program module for this. It uses a quirk of how KRL handles divsion remainders between Integer and Real variables.


    I have the ENUM type variable EvenOrOdd declared globally in the .DAT file of the module:

    Code
    GLOBAL ENUM _EvenOrOdd Even,Odd
    DECL GLOBAL _EvenOrOdd EvenOrOdd

Advertising from our partners