Testing a number to see if it's even.

Blitz3D Forums/Blitz3D Beginners Area/Testing a number to see if it's even.

VP(Posted 2005) [#1]
Is there a better way to return True or False for even-ness than:
iseven% = (num% Shr 1)-((num%+1) Shr 1)

In a context where num% may be MilliSecs()/1000 (to create flashing text, for instance)?


big10p(Posted 2005) [#2]
is_odd% = num% And 1

or:

is_even% = Not(num% And 1)



VP(Posted 2005) [#3]
That would be the logical, rather than mathematical solution.

Thanks biggie :D


octothorpe(Posted 2005) [#4]
The mathematical solution (which would be slower than the bitwise solution given above) would use the MOD operator.

function is_even(num%)
	if (num MOD 2 = 0) then return true else return false
end function