To work out if a value is even...

Just starting out? Need help? Post your questions and find answers here.
kawasaki
Enthusiast
Enthusiast
Posts: 182
Joined: Thu Oct 16, 2003 8:09 pm

To work out if a value is even...

Post 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
milan1612
Addict
Addict
Posts: 894
Joined: Thu Apr 05, 2007 12:15 am
Location: Nuremberg, Germany
Contact:

Post 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
Windows 7 & PureBasic 4.4
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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 )
oh... and have a nice day.
moogle
Enthusiast
Enthusiast
Posts: 372
Joined: Tue Feb 14, 2006 9:27 pm
Location: London, UK

Post 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.
Image
kawasaki
Enthusiast
Enthusiast
Posts: 182
Joined: Thu Oct 16, 2003 8:09 pm

Post 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
moogle
Enthusiast
Enthusiast
Posts: 372
Joined: Tue Feb 14, 2006 9:27 pm
Location: London, UK

Post 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
Image
kawasaki
Enthusiast
Enthusiast
Posts: 182
Joined: Thu Oct 16, 2003 8:09 pm

Post by kawasaki »

Moogle,

You are right, I do apologise.


Thanks :)


Mike
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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)
oh... and have a nice day.
moogle
Enthusiast
Enthusiast
Posts: 372
Joined: Tue Feb 14, 2006 9:27 pm
Location: London, UK

Post 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
Image
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

The PB compiler optimizes modulo by a power of two into an and, so they should perform identically.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

ok... so the test was obsolete... :lol:
oh... and have a nice day.
moogle
Enthusiast
Enthusiast
Posts: 372
Joined: Tue Feb 14, 2006 9:27 pm
Location: London, UK

Post by moogle »

It's always good to know :P
Image
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Post 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).
Post Reply