Odd or Even numbers
Odd or Even numbers
How can I get PB to distinguish between Odd and Even numbers?
One way (with integers): AND with 1 as the low bit is always 0 for evens and 1 for odds. Zero is "even".
Code: Select all
a.l=0
If a & 1
Debug "odd"
Else
Debug "even"
EndIf
Dare2 cut down to size
Thanks for that.
If a user inputs a number (single digit only) 0 to 9.
I want to be able to add 2 to it until it rotates back to where it started.
eg. Inputed number 4 then it goes 6 8 0 2 back to 4. If odd inputed no. 5 then it will go 7 9 1 3 back to five. Make sense?
I want to be able to add 2 to it until it rotates back to where it started.
eg. Inputed number 4 then it goes 6 8 0 2 back to 4. If odd inputed no. 5 then it will go 7 9 1 3 back to five. Make sense?
What you said makes sense however the explanation/concept has nothing to put it into context - which may be relevant to approach. My first thought was a tight loop:
This is a pretty simplistic method. Regardless, it is an approach that can be used in a broader context.
Hope it is useful. If not, keep asking and a sharper mind than mine will be along shortly, instantly understand what you're after and give you the good oil.
Code: Select all
num = 1 ; or whatever
initialNum = num
Repeat
Debug Str(num) + " " + Str(initialNum) ; Or whatever is being done with it
num + 2
If num > 9
num - 10
EndIf
Until num = initialNum
Hope it is useful. If not, keep asking and a sharper mind than mine will be along shortly, instantly understand what you're after and give you the good oil.

Dare2 cut down to size
Thank's Heaps !
That's great stuff. Thanks heaps...
The second row runs into 2 digits. And won't rotate back to start.
Any ideas ?
Any ideas ?
Code: Select all
num5 = 5 : num6 = 8 ; or whatever
initialNum6 = num6
initialNum5 = num5
Repeat
Debug Str(num6) + " - " + Str(num5); + Str(initialNum) ; Or whatever is being done with it
num6 + 2 : num5 + 2
If num6 > 9 : num6 - 10
If num5 > 9 : num5 - 10
EndIf
EndIf
Until num6 = initialNum6 : Or num5 = initialnum5
I'm just using the MOD operator, see helpfile. Not tested, but should work with all numbers (greater and smaller than 0), incl. 0.
Code: Select all
Macro isOdd(number)
(number % 2 AND 1)
EndMacro
PB 4.30
Code: Select all
onErrorGoto(?Fred)
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
It's under Variables, Types and Operators. Look for the percent sign operator:
PB docs wrote:Modulo. Returns the remainder of the RHS by LHS integer division.
Example:
a=16 % 2 ; The value of a will be 0 as 16/2 = 8 (no remainder)
b=17 % 2 ; The value of a will be 1 as 17/2 = 8*2+1 (1 is remaining)
BERESHEIT
All in all I come to the cnclusion, that my code and Derek's Code are the fastest.
But my one is easier.
I expanded my

But my one is easier.

I expanded my
Code: Select all
Macro isOdd(number)
(number & 1)
EndMacro
Macro isEven(number)
(number % 2 XOr 1)
EndMacro
Macro isEven_2(number)
(Not (number % 2))
EndMacro
;- Testing
Debug isOdd(23)
Debug isOdd(24)
Debug isEven(23)
Debug isEven(24)
Debug isEven_2(23)
Debug isEven_2(24)
- Added isEven_2(), which is working without "XOr 1"
Changed isOdd(), now working bitwise instead of using modulo (thx2kaeruGaman)
Last edited by AND51 on Mon Dec 04, 2006 6:15 pm, edited 2 times in total.
PB 4.30
Code: Select all
onErrorGoto(?Fred)
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Not really. My macro just returns 1 or 0.
Let's imagine, we've got 20 % 9, result would be 2.
No, I made my code as every PB Procedure, it just returns 0 or 1. It's better to compare in If's.
@ PeterGums: What about feedback?
Let's imagine, we've got 20 % 9, result would be 2.
No, I made my code as every PB Procedure, it just returns 0 or 1. It's better to compare in If's.
@ PeterGums: What about feedback?

PB 4.30
Code: Select all
onErrorGoto(?Fred)
Oh, ok, now I see what your aiming at:
my code without "And 1" can only return 0 or 1... And that can be achived even without "And 1".... OK, I will leave it out.
my code without "And 1" can only return 0 or 1... And that can be achived even without "And 1".... OK, I will leave it out.
PB 4.30
Code: Select all
onErrorGoto(?Fred)