BITs in as3 increase speed

Source: Internet
Author: User
Tags bitwise bitwise operators

The median operation in as3 is very fast. Here we list a set of code snippets that can accelerate some computing speeds. I won't explain what bitwise operators are or how to use them. I just want to tell you how they work. Here is an excellent article titled 'bitwise operation in c' in gamedev.net'
.

If you know any tips not listed below, leave a comment or send me an email. All of these are based on as3.

The number of left shifts is equal to the power multiplied by 2.

(Left bit shifting to multiply by any power of two)
// Moving a value to the left is equivalent to multiplying this value by 2. Floating Point Numbers are converted to Integers by rounding all digits after the decimal point.
About 300% faster

X = x * 2;
X = x * 64;
// Equivalent:
X = x <1;
X = x <6;


The right displacement is equal to dividing by the power of 2.
(Right bit shifting to divide by any power of two)
// Shifting a value to the right is equivalent to dividing it by 2 and removing the remainder. Floating Point Numbers are converted to Integers by rounding all digits after the decimal point.
About 350% faster

X = X/2;
X = x/64;
// Equivalent:
X = x> 1;
X = x> 6;


Number to Integer Conversion

In as3, INT (X) is faster than 10%. However, the bitwise operation version works better in as2.

X = int (1.232)
// Equivalent:
X = 1.232> 0;


Extract color components

Not exactly a trick. It is a normal method (not really a trick, but the regular way of extracting values using bit masking and shifting .)

// 24bit
VaR color: uint = 0x336699;
VaR R: uint = color> 16;
VaR G: uint = color> 8 & 0xff;
VaR B: uint = color & 0xff;
// 32bit
VaR color: uint = 0xff3000099;
VaR A: uint = color >>> 24;
VaR R: uint = color >>> 16 & 0xff;
VaR G: uint = color >>> 8 & 0xff;
VaR B: uint = color & 0xff;


Merge color components

Replace values to the correct position and combine them ('shift up' the values into the correct position and combine them .)

// 24bit
VaR R: uint = 0x33;
VaR G: uint = 0x66;
VaR B: uint = 0x99;
VaR color: uint = r <16 | G <8 | B;
// 32bit
VaR A: uint = 0xff;
VaR R: uint = 0x33;
VaR G: uint = 0x66;
VaR B: uint = 0x99;
VaR color: uint = A <24 | r <16 | G <8 | B;


Use an exclusive or operation to exchange integers without using temporary variables

Very cute tips. I have a detailed explanation in the link at the top of this page. It's faster than 20%.

VaR T: Int =;
A = B;
B = T;
// Equivalent:
A ^ = B;
B ^ =;
A ^ = B;


Auto-increment/auto-Increment
(Increment/decrement)

This is much slower than before, but it is a good way to blur your code ;-)

I = -~ I; // I ++
I = ~ -I; // I --


Invert
(Sign flipping using not or XOR)

Another strange thing is that this is actually 300% faster!

I =-I;
// Equivalent:
I = ~ I + 1;
// Or
I = (I ^-1) + 1;


Use bitwise AND to quickly modulo
(Fast modulo operation using bitwise AND)

If the divisor is the power of 2, perform the modulo operation as follows:

Modulus = numerator & (divisor-1 );

This is about 600% faster

X = 131% 4;
// Equivalent:
X = 131 & (4-1 );


Check if it is an even number
(Check if an integer is even/uneven using bitwise AND)

600% faster

Iseven = (I % 2) = 0;
// Equivalent:
Iseven = (I & 1) = 0;


Absolute Value

Forget math. Abs () (forget math. Abs () for time critical code .)

Version 1 is 2500% faster than math. Abs (), and Version 2 is 20% faster than Version 1!

// Version 1
I = x <0? -X: X;
// Version 2
I = (x ^ (x> 31)-(x> 31 );


// The last two do not understand

Comparing two integers for equal sign
This is 35% faster.

Eqsign = a * B> 0;
// Equals:
Eqsign = a ^ B> = 0;


Fast Color Conversion from r5g5b5 to r8g8b8 pixel format using shifts

R8 = (R5 <3) | (R5> 2)
G8 = (R5 <3) | (R5> 2)
B8 = (R5 <3) | (R5> 2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.