# when are the '&' operator used?

• 01-16-2001, 05:20 AM
sPiNKs
im a newbie scripter who just started to get serious about scripting,, =) and i was looking all the available operators and stuph, then i came accross the & operator,,

by reading about thousands of times and experimenting with the operator in flash itself, now i understand how the operator functions.

But now im so curious, when can this operator be used? I just couln't think of any instances how this operator can be useful to me,

so, when do you guys use this operator? plz tell me!

• 01-16-2001, 06:58 AM
Brother
Quote:

Originally posted by sPiNKs

But now im so curious, when can this operator be used? I just couln't think of any instances how this operator can be useful to me,

so, when do you guys use this operator? plz tell me!

Ok, sPiNKs, once you figured out how this bitwise operator functions, heres the application:

In old times, when you had to write in pure code if you want it run faster, AND was used as kind of modulo.
For example, 19 AND 15=19 mod 16=3. This works only if divider is an integer positive power of 2, as in my example 16=2^4. In processor level, much of stuff is measured in the power of 2.

The same way, you can test a single bit in the variable:
if (myVar & 16 > 0) {
trace ("5th bit is set");
}
This could be applied in some boolean array management. This way you can save boolean status of array items in single number.

Another application is pixel masking. If you have B&W image defined by array of numbers representing the lines of pixels with 1s where pixel is, you can use bitwise AND to hide certain pixels. Practically it was used imaging mouse pointer, where pixels "under" the pointer are ruled out before copying the cursor icon.

Actually, in Flash, speed gain will be insignifficant, if any. However, if your script has complex math, it will be much quicker if you optimize it and use bitwise operators instead of arrays of booleans.