Page 1 of 1

To work out if a value is even...

Posted: Sun Aug 10, 2008 9:24 pm
by kawasaki
Hello,

I'm stuck at a junction with what I am currently working on (which will be released to the community slowly but surely), and that junction is mathematics.

Is there a very simple way to work out if a number is even or odd?

I an considering using a brute-force approach which is to run a loop, adding 2 to a variable each time, checking to see if it equals the input number (for even) or jumps over it (odd), but obvisouly that method is not very efficient.

I have trolled a search through the forum, with no results.

Thankyou kindly,

Mike

Posted: Sun Aug 10, 2008 9:26 pm
by milan1612
I'm not sure if I understand you correctly, but anyway...

Code: Select all

number.l = 5
If number % 2 = 0
  ;even
Else
  ;odd
EndIf

Posted: Sun Aug 10, 2008 9:34 pm
by Kaeru Gaman
or with & (bitwise And)

Code: Select all

Macro Odd( Exp )
  ( Int( Exp ) & 1 )
EndMacro

Debug Odd( 5 )
Debug Odd( 6 )
Debug Odd( 7 )
Debug Odd( 8 )
the Int() makes sure, that an Integer will pe processed,
to avoid Errors when entering an not previous known expression.

in a direct If, you can also use the & directly with milan's construct:

Code: Select all

number.l = 5
If number & 1 = 0
  ;even
Else
  ;odd
EndIf
or use Modulo for a Macro:

Code: Select all

Macro Odd( Exp )
  ( Int( Exp ) % 2 )
EndMacro

Debug Odd( 5 )
Debug Odd( 6 )
Debug Odd( 7 )
Debug Odd( 8 )

Posted: Mon Aug 11, 2008 1:12 am
by moogle
From some PHP code

Same as kaeru's sort of.

Code: Select all

For i=0 To 40
	x=(i & 1)
	Select x
		Case 0 ;Even
			Debug Str(i) + " is Even"
		Case 1 ; Odd
			Debug Str(i) + " is Odd"
	EndSelect
Next i
Using the Bitwise AND op.

Posted: Mon Aug 11, 2008 5:38 pm
by kawasaki
Hello Again,

Much thanks for the macro, Kaeru, it works perfectly.

@ Moogle - That was pretty much the technique I adopted initially, but the only problem being, that the program could be processing thousands of variables a second, with the value ranging from 0 to 2147483647, so creating a loop to incorporate that range would be quite hard on the program when used excessively, especially when speed is a key element I wish to provide on the software itself.



Regards,

Mike

Posted: Mon Aug 11, 2008 6:08 pm
by moogle
kawasaki wrote: @ Moogle - That was pretty much the technique I adopted initially, but the only problem being, that the program could be processing thousands of variables a second, with the value ranging from 0 to 2147483647, so creating a loop to incorporate that range would be quite hard on the program when used excessively, especially when speed is a key element I wish to provide on the software itself.



Regards,

Mike
Oh no you don't need the for loop. It's just demonstrating the (Int & 1) method.

Seems the modulo and the bitwise AND perform the same. Just the AND method needs a little less characters :P

Posted: Mon Aug 11, 2008 6:19 pm
by kawasaki
Moogle,

You are right, I do apologise.


Thanks :)


Mike

Posted: Mon Aug 11, 2008 6:29 pm
by Kaeru Gaman
> Seems the modulo and the bitwise AND perform the same. Just the AND method needs a little less characters

the AND does an AND, the Modulo does an Integer-Division and passes the leftover-register as result.

I don't know wich is faster, needs a performance test to decide.
since the operation should be very fast, we should need millions of opertations for a check...

...

seems to be almost the same duration...

Code: Select all

Define sign.l, n.l, t.l

timer1 = ElapsedMilliseconds()
For t=0 To 99
  For n=0 To $FFFFFF
    sign = (n & 1) 
  Next
Next
timer1 = ElapsedMilliseconds() - timer1

timer2 = ElapsedMilliseconds()
For t=0 To 99
  For n=0 To $FFFFFF
    sign = (n % 2) 
  Next
Next
timer2 = ElapsedMilliseconds() - timer2

out$ = "Bitwise And: " + Str(timer1) + "ms" + #CRLF$ + #CRLF$
out$ + "Modulo: " + Str(timer2) + "ms"

MessageRequester("Speed Test", out$, #MB_ICONINFORMATION)

Posted: Mon Aug 11, 2008 7:13 pm
by moogle
Kaeru Gaman wrote:> Seems the modulo and the bitwise AND perform the same. Just the AND method needs a little less characters

the AND does an AND, the Modulo does an Integer-Division and passes the leftover-register as result.
I know I meant in the sense of performing and odd even check it's just the same.

In your code too I get the same time for each too like mine :D

Posted: Mon Aug 11, 2008 7:42 pm
by Trond
The PB compiler optimizes modulo by a power of two into an and, so they should perform identically.

Posted: Mon Aug 11, 2008 8:13 pm
by Kaeru Gaman
ok... so the test was obsolete... :lol:

Posted: Mon Aug 11, 2008 8:20 pm
by moogle
It's always good to know :P

Posted: Mon Aug 11, 2008 8:21 pm
by Seldon
Trond wrote:The PB compiler optimizes modulo by a power of two into an and, so they should perform identically.
Ah ! I hate when compilers do optimize all for you ! :D Now I know why I had the same time results using '% x' and '& x-1' (where x is a power of two).