Finding Odd/Even values Efficiently

Alright, so I’m used to doing this using the modulus operator ( % ), but we all know that this operator is rather heavy in any language, especially Actionscript.

Well I was trying to put together some examples for you guys on how to use the bitwise operators, which incidentally are the fastest operators in Actionscript. In fact they are extremely fast in comparison to the standard operators.

Anyway while I was putting some examples together I happened to come across something that I never thought about, but it makes perfect sense.

The bitwise AND operator ( & ) takes two bits and returns one of the following:

These bitwise operators work on numbers in their Binary form. I’m sure all of you know what binary is, and how it works, I will only go over it lightly.

Now every has at least a value of 1, whether it is a negative or positive doesn’t matter. Now the way we read numbers, the digit furthest to the right just before the decimal place is the ONEs position, this is the first bit in any number (which might seem odd but in binary we read digits from right to left, not left to rigth as we are used to doing in decimal)

Let’s take a look at an example

You have the number 5 in decimal… in binary 5 is represented as 101, if you don’t know why here is a quick look…

Okay so when you are using the bitwise & operator you have to operands…

operand1 & operand2

These operands are bits, now we know that the & operator will compare the two bits and if they are both 1 then it will return 1 otherwise it will return 0. Now when using the operator in this simple form it will do comparisons on the first bit (ONEs). Let’s take a look at a few examples of even and odd numbers in their binary form…

Odd:
Decimal: 5
Binary: 101

**Even:
**Deicmal: 6
Binary: 110

Now if you look closely you’ll see that in odd numbers the first bit is always a 1, and in even numbers the first bit is always a 0.

With that said if you were to & the first bit using the value 1 (number & 1), this would return a 0 if the number’s first bit were not a 1, and would return a 1 if it was a 1.

Eh… that’s all a little confusing if you’ve never dealt with binary numbers or their bitwise operators.

Let’s look at some code…

Open Flash and start a new file, open the actions panel and put in the following.


trace( 345 & 1 ); // outputs 1
trace( 346 & 1 ); // outputs 0

Now let’s say we were to compare the result with the number 0…


trace( (345 & 1) == 0 ) // returns false
trace( (346 & 1) == 0 ) // returns true

Here we have the values successfully returning whether they are even or odd… we could wrap this in an easy to reuse function as well… although method invocation would slow down the process it’s not by enough to be to worried about, taking the same precautions as when you usually write functions. Am I using this in alot of different places in my code? if so then use a function if not just use the equation in plain form.

isEven method…


// AS 2
function isEven( value:Number ) : Boolean
{
return (value & 1) == 0;
}

// AS 1
 function isEven( value )
 {
 return (value & 1) == 0;
 }

Usage…


var myVar = isEven( 15 );

trace(myVar);// outputs false

I hope that this made some sense… I have hardly slept as I was at a concert in Ventura last night… I may be back to revise when I am more awake.

TakeCare.
_Michael

it’s something new and interesting

being mathematicly most easy for me to do
number % 2 == 0

or in function
function isEven2(value:Number) : Boolean
{
return (value % 2) == 0;
}

I test your and this mathematicly intuitive

they are almost same speed so I prefer intuitive one

but good explanation on bits
some good stuff can be done with manipulationg bits and numbers

[AS]

function isEven( value:Number ) : Boolean
{
return (value & 1) == 0;
}

function isEven2(value:Number) : Boolean
{
return (value % 2) == 0;
}

startTime = getTimer();

for (i=0; i<200000; i++) {
isEven(i);
}

numTimeElapsed = getTimer()- startTime;
trace(numTimeElapsed);
qtyTimeSecs = Math.floor(numTimeElapsed/1000)%60;
qtyTimeMins = Math.floor(numTimeElapsed/60000)%60;
qtyTimeHrs = Math.floor(numTimeElapsed/3600000);
qtyTimeHdths = Math.floor(numTimeElapsed/10)%100;

strTimeSecs = (String(qtyTimeSecs).length<2) ? “0”+qtyTimeSecs : qtyTimeSecs;
strTimeMins = (String(qtyTimeMins).length<2) ? “0”+qtyTimeMins : qtyTimeMins;
strTimeHrs = (String(qtyTimeHrs).length<2) ? “0”+qtyTimeHrs : qtyTimeHrs;
strTimeHdths = (String(qtyTimeHdths).length<2) ? “0”+qtyTimeHdths : qtyTimeHdths;

startTimeString = strTimeHrs+":"+strTimeMins+":"+strTimeSecs+ “:” + strTimeHdths;

trace(startTimeString);

startTime = getTimer();

for (i=0; i<200000; i++) {
isEven2(i);
}

numTimeElapsed = getTimer()- startTime;
trace(numTimeElapsed);
qtyTimeSecs = Math.floor(numTimeElapsed/1000)%60;
qtyTimeMins = Math.floor(numTimeElapsed/60000)%60;
qtyTimeHrs = Math.floor(numTimeElapsed/3600000);
qtyTimeHdths = Math.floor(numTimeElapsed/10)%100;

strTimeSecs = (String(qtyTimeSecs).length<2) ? “0”+qtyTimeSecs : qtyTimeSecs;
strTimeMins = (String(qtyTimeMins).length<2) ? “0”+qtyTimeMins : qtyTimeMins;
strTimeHrs = (String(qtyTimeHrs).length<2) ? “0”+qtyTimeHrs : qtyTimeHrs;
strTimeHdths = (String(qtyTimeHdths).length<2) ? “0”+qtyTimeHdths : qtyTimeHdths;

startTimeString = strTimeHrs+":"+strTimeMins+":"+strTimeSecs+ “:” + strTimeHdths;

trace(startTimeString);

stop();
[/AS]

Wow! That is one comprehensive explanation! Sweet… thanks.

I guess Flash doesn’t have non-short-circuiting boolean operators like | and & (well, that’s used for something else… so I guess it isn’t). Strange.

Could you explain to me how you write letters in binary :smiley:

edit:/ WAITTT, all ascii characters have decimal values, you just do the same thing? How do you differentiate between a number and a character then…

Numbers 0-9 are ascii 48-57, the letters are from 65-90.

25 letters?

harish, compilers take bussiness of that because every compiler do tables for variables, and reserve memory space for those variables

I could write a book about compilers here but it’s nothing needed here
:slight_smile: