## 位运算和二进制

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

00000001 = 1
00000010 = 2
00000100 = 4
00001000 = 8
00010000 = 16
00100000 = 32
01000000 = 64
10000000 = 128

00000010 = 2
00000100 = 4
00000110 = 6

flags = flags | 1;flags

"|" 运算符是 位运算的 “或”，意思是上述说明的 “或” 1 并赋值回 flags。如果你还记得之前的情况，使用一个 1 将设置第一个位。如果我们想要设置第二个位，我们要这样做：

flags = flags | 2;

010110100
110011001
110111101

00 | 00 = 00
00 | 01 = 01
01 | 01 = 01
01 | 00 = 01

flags = flags | 5;

01110010101
00110000100
00110000100

00 & 00 = 00
01 & 00 = 00
00 & 01 = 00
01 & 01 = 01

00000000000000000000001000111011
11111111111111111111111111111010
00000000000000000000001000111010

“非” 运算就像它说的一样 - “非” 那些位。下面是 “非” 运算的真值表。

~00 = 11
~01 = 10
~10 = 01
~11 = 00

a = a & ~5;

0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0

a = a ^ 1;

00000001
00000001
00000010

01011011 = 91
00101101 = 45
10001000 = 136

00000100 * 2 = 00001000 = 8

101 = 01100101 * 2 = 11001010 = 202

00000001 << 1 = 000000010 = 2
00000001 << 2 = 000000100 = 4
00000001 << 3 = 000001000 = 8
00000001 << 4 = 000010000 = 16
00000001 << 5 = 000100000 = 32
00000001 << 6 = 001000000 = 64
00000001 << 7 = 010000000 = 128
00000001 << 8 = 100000000 = 256

A = A | (1<<27)

ACTIVE= 27;
BOOM = 0;
A = A | (1<<ACTIVE) | (1<<BOOM);

A = A | 13421772;

A = A & ~((1<<ACTIVE) | (1<<BOOM));

64 / 32 = 01000000 >> 5 = 00000010

68 / 32 = 01000100 >> 5 = 00000010

var X_index = x>>5;
var Y_index = y>>5;
cell_data = mygrid[# X_index,Y_index];

var r = x & 31
var X_Index = x>>5;

var r = x mod 32;
var X_Index = floor(x/32);

## 举例

GameMaker Studio 2 的开发者们经常使用 place_free() 函数，当发生碰撞时，尝试通过在 x 或 y位置(或其他位置)上循环缓慢移动物体，同时继续执行该函数，或者使用更为方便的 move_outside_all() 函数。

x = x&~31;

if( (key_id & door_id) !=0 ) { opendoor(); }

counter = (counter+1)&15;

return (argument0&(argument0-1))==0;

return (argument0 + (argument1-1)) & ~(argument1-1);