# Odd or even numbers

**URL:** <https://tech-community.robotics.abb.com/t/odd-or-even-numbers/6354>\
**Category:** RobotStudio\
**Created:** [November 6, 2014, 8:13am UTC](https://tech-community.robotics.abb.com/t/odd-or-even-numbers/6354 "2014-11-06T08:13:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pavpav](https://avatars.discourse-cdn.com/v4/letter/p/e99b99/32.png) [@pavpav](https://tech-community.robotics.abb.com/u/pavpav)\
**Post date:** [November 6, 2014, 8:13am UTC](https://tech-community.robotics.abb.com/t/odd-or-even-numbers/6354/1 "2014-11-06T08:13:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![blind3rr](https://avatars.discourse-cdn.com/v4/letter/b/97f17d/32.png) [@blind3rr](https://tech-community.robotics.abb.com/u/blind3rr)\
**Post date:** [November 6, 2014, 8:57am UTC](https://tech-community.robotics.abb.com/t/odd-or-even-numbers/6354/2 "2014-11-06T08:57:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pavpav](https://avatars.discourse-cdn.com/v4/letter/p/e99b99/32.png) [@pavpav](https://tech-community.robotics.abb.com/u/pavpav)\
**Post date:** [November 6, 2014, 9:14am UTC](https://tech-community.robotics.abb.com/t/odd-or-even-numbers/6354/3 "2014-11-06T09:14:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![B\_Baan](https://avatars.discourse-cdn.com/v4/letter/b/439d5e/32.png) [@B\_Baan](https://tech-community.robotics.abb.com/u/B_Baan)\
**Post date:** [November 6, 2014, 2:12pm UTC](https://tech-community.robotics.abb.com/t/odd-or-even-numbers/6354/4 "2014-11-06T14:12:20Z")

</div>

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:

> **[Modulo](https://en.wikipedia.org/wiki/Modulo_operation)**
>
> In computing and mathematics, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another, the latter being called the modulus of the operation.
> Given two positive numbers a and n, a modulo n (often abbreviated as a mod n) is the remainder of the Euclidean division of a by n, where a is the dividend and n is the divisor.
> For example, the expression "5 mod 2" evaluates to 1, because 5 divided by 2 has a quotient of 2 and a remainder of 1, ...

Hope this helps

---

<div class="post-metadata">

**Author:** ![pavpav](https://avatars.discourse-cdn.com/v4/letter/p/e99b99/32.png) [@pavpav](https://tech-community.robotics.abb.com/u/pavpav)\
**Post date:** [November 6, 2014, 3:08pm UTC](https://tech-community.robotics.abb.com/t/odd-or-even-numbers/6354/5 "2014-11-06T15:08:59Z")

</div>

Ah thx the remainder after a quotient

---

<div class="post-metadata">

**Author:** ![Kristo](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/kristo/32/3223_2.png) [@Kristo](https://tech-community.robotics.abb.com/u/Kristo)\
**Post date:** [February 20, 2018, 3:05pm UTC](https://tech-community.robotics.abb.com/t/odd-or-even-numbers/6354/6 "2018-02-20T15:05:15Z")

</div>

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
