1.
x = x * 2;
x = x * 64;
//改为:
x = x << 1; // 2 = 21
x = x << 6; // 64 = 26
2.
x = x / 2;
x = x / 64;
//改为:
x = x >> 1;// 2 = 21
x = x >> 6;// 64 = 26
3.
x = int(1.232)
//改为:
x = 1.232 >> 0;
4.
var t:int = a;
a = b;
b = t;
//equals:
a = a^b;
b = a^b;
a = a^b;
5.
i = -i;
//改为
i = ~i + 1; // NOT 写法
//或
i = (i ^ -1) + 1; // XOR 写法
6.
x = 131 % 4;
//equals:
x = 131 & (4 - 1);
7.
isEven = (i % 2) == 0;
//equals:
isEven = (i & 1) == 0;
8.
//写法1
i = x < 0 ? -x : x;
//写法2
i = (x ^ (x >> 31)) - (x >> 31);
//写法3
i=x^(~(x>>31)+1)+(x>>31);
9.
eqSign = a * b > 0;
//equals:
eqSign = a ^ b > 0;
其它位运算技巧
1. RGB
var 24bitColor:uint = 0xff00cc;
var r:uint = 24bitColor >> 16;
var g:uint = 24bitColor >> 8 & 0xFF;
var b:uint = 24bitColor & 0xFF;
2. RGB
var r:uint = 0xff;
var g:uint = 0x00;
var b:uint = 0xcc;
var 24bitColor:uint = r << 16 | g << 8 | b;