To work out if a value is even...
To work out if a value is even...
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
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
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
- Kaeru Gaman
- Addict
- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
or with & (bitwise And)
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:
or use Modulo for a Macro:
Code: Select all
Macro Odd( Exp )
( Int( Exp ) & 1 )
EndMacro
Debug Odd( 5 )
Debug Odd( 6 )
Debug Odd( 7 )
Debug Odd( 8 )
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
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.
From some PHP code
Same as kaeru's sort of.
Using the Bitwise AND op.
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

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
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
Oh no you don't need the for loop. It's just demonstrating the (Int & 1) method.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
Seems the modulo and the bitwise AND perform the same. Just the AND method needs a little less characters


- Kaeru Gaman
- Addict
- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
> 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...
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.
I know I meant in the sense of performing and odd even check it's just the same.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.
In your code too I get the same time for each too like mine


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