Odd or even numbers

Hi

I would like to check if a value is odd or even, does anyone have a good solution for this in RAPID?

I know its possible to divide a value by 2 and count the amount of decimals, but i cannot find a function that can count decimals.

Thank you

FUNC num OddEven(num x)
VAR num nAux;

nAux := Trunc(x) MOD 2;
TEST nAux
CASE 0:
! Value is even
RETURN 2;
CASE 1:
! Value is odd
RETURN 1;
ENDTEST
ENDFUNC

Something like this?

Thx

I read about MOD and the way i see it is that it counts the amount of numbers a value consists of?

11/5 is 2,2 so MOD will be 2 because there is two numbers?

You clearly don’t understand MOD yet, but you know it’s function.

This page contains an explanation of the mathematics behind the MOD (A.K.A. Modulo) instruction:

Hope this helps

Ah thx the remainder after a quotient

One way is to check first bit of the number. If the bit is 1 than the number is odd, if it’s 0, the number is even. Example code:

!I converted the num to dnum, than it’s possible to check higher value number than 0…255.

FUNC bool checkIsNumberOddOrNot(num inputNum)
VAR dnum inputDnum;

inputDnum:=numToDnum(inputNum);

IF BitCheckDnum(inputDnum, 1) = TRUE THEN
!first bit is true, it’s an odd number
RETURN TRUE;
ELSE
!first bit is false, it’s not an odd number
RETURN FALSE;
ENDIF

ENDFUNC

Br,
Kristo