Shop
OBEX
P1 Docs
P2 Docs
Learn
Events
Display even(odd) number only — Parallax Forums
toggle menu
Categories
Discussions
Sign In
·
Register
Sign In
·
Register
Categories
Discussions
Sign In
·
Register
×
Home
›
BASIC Stamp
Display even(odd) number only
Mohamed Refky
Posts:
47
2006-03-22 16:41
edited 2006-03-22 17:04
in
BASIC Stamp
Hello,
How to determine that a number is odd number or even number?
Thank You
Comments
Paul Baker
Posts:
6,351
2006-03-22 16:47
edited 2006-03-22 16:47
IF X//2 == 1
'number is odd
ELSE
'number is even
ENDIF
you can also do it by testing bit 0 of the value, it its a 0 its even if its a 1 its odd, this method is actually
·
quicker than using modulo in the first method.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·
1+1=10
Jon Williams
Posts:
6,491
2006-03-22 16:49
edited 2006-03-22 16:49
Here's how to implement Paul's BIT0 suggestion:
·
IF (myValue.BIT0 = 1) THEN
···
' number is odd
·
ELSE
···
' number is even
·
ENDIF
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Paul Baker
Posts:
6,351
2006-03-22 17:04
edited 2006-03-22 17:04
Thanks Jon, I see my work with the propeller is starting to crowd out PBASICs syntax, as my above IF-THEN example has some syntax errors.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·
1+1=10
Sign In
or
Register
to comment.
Comments
'number is odd
ELSE
'number is even
ENDIF
you can also do it by testing bit 0 of the value, it its a 0 its even if its a 1 its odd, this method is actually·quicker than using modulo in the first method.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10
· IF (myValue.BIT0 = 1) THEN
··· ' number is odd
· ELSE
··· ' number is even
· ENDIF
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10