Is there a way to test if a number is even?

Blitz3D Forums/Blitz3D Beginners Area/Is there a way to test if a number is even?

n8r2k(Posted 2005) [#1]
Read the title then please answer me. If theres not a blitz function, can anyone think of a way to do this?


Sledge(Posted 2005) [#2]
abs(x) mod 2 has to equal zero for x to be even, a result of one signifies that x is odd.


n8r2k(Posted 2005) [#3]
First, thanks for helping me and second, What are you doing there though can you explain?


n8r2k(Posted 2005) [#4]
nvm i looked at the Blitz Docs


sswift(Posted 2005) [#5]
If the number is an integer you can do this:

If (A AND 1) = 0 Then Even

What that does is a bitwise and of the binary representation of A and the binary representation of the value 1.

The binary representation of the value 1 is all 0's, except the first bit, which is 1.

The first bit adds either 0 or 1 to the total number, the second bit adds either 0 or 2... the third add either 0 or 4...

Ie:

0 = 000
1 = 001
2 = 010
3 = 011
4 = 100


If we AND two integers together, then we get a number which only has bits set to 1 where they are 1 in BOTH numbers. Since we only have one bit here set to 1, then the result can only be based on that bit. And that bit controls whether 0 or 1 is added to the number, so out result will either be 0 or 1 depeding on whether A as a 1 at that bit position...

And A will only have a 1 at that bit position if it is ODD, as the table above shows.


Now, if you want to do floats, that's more complicated, and I'm not sure if a number like 1.1 can even be consideerd odd or even.

But if you only want to use the whole part of the float, the part to the left of the decimal point then you can do this:

If (Floor(A#) AND 1) = 0 Then Even


And that will cut off everything after the decimal point before doing the comparison.


sswift(Posted 2005) [#6]
Sledge:
The abs() really isn't even neccessary to do it that way... The number is even if A mod 2 is 0, and it is odd if A mod 2 is not 0. No need to make sure it is never -1.


Sledge(Posted 2005) [#7]

I'm not sure if a number like 1.1 can even be consideerd odd or even



The terms "odd" and "even" only apply to integers. That's a real neat method though sswifty.


Sledge(Posted 2005) [#8]

The abs() really isn't even neccessary to do it that way... The number is even if A mod 2 is 0, and it is odd if A mod 2 is not 0. No need to make sure it is never -1



Now you're introducing logic into programming - where's your sense of Microsoft?


n8r2k(Posted 2005) [#9]
Thanks Sledge.


Ice9(Posted 2005) [#10]
AND Faster
MOD slower
Both work if speed isn't a factor


n8r2k(Posted 2005) [#11]
I still don't understand how the AND works so i think i will us the MOD


Rook Zimbabwe(Posted 2005) [#12]
Hey n8r... your www.n8r2k.tk link is borken?
-RZ


Rottbott(Posted 2005) [#13]
n8r2k,

The number (call it n) is stored in binary as a 4 byte integer in memory. Say that n = 3, it will look like this in binary:

11

That's 2 + 1 = 3. If n = 6 it will look like this:

110

That's 4 + 2 = 6. If n = 1 it will look like this:

1

1 = 1. So when you do an AND it compares each binary digit (bit), and if they are 1 in both numbers that digit is 1 in the result. If either number has a 0 for that digit, it is 0 in the result. Therefore:

011 AND 001 = 001
110 AND 001 = 000

Here's the same thing in decimal:

3 AND 1 = 1
6 AND 1 = 0

Since any even number will have a 0 for the rightmost bit, and any odd number will have 1, you will get a result of 1 for any odd number and a result of 0 for any even number.

If this doesn't make sense, read up on how binary numbers work and reread :)


n8r2k(Posted 2005) [#14]
Okay i understand now. Thanks RottBott. And Rook, my links shouldn't be. Try refreshing the page and if that doesnt work, you can go manually to http://dragonflame36.tripod.com/n8r2k/ . Thanks all.


_PJ_(Posted 2005) [#15]
heh
And I was gonna suggest something like if (2*(x%/2))=(2*(x#/2))

um...............

I do like that logical stuff though.