lua不同版本的位操作

lt發表於2016-10-12

5.1 使用外部bitlib庫

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> require "bit"
> a=12
> b=9
> print(bit.band(a,b))
8
> print(bit.bor(a,b))
13
> print(bit.bxor(a,b))
5
> print(bit.bnot(a))
-13
> print(bit.rshift(a,2))
3
> print(bit.lshift(b,3))
72

5.2 使用內建bit32庫

Lua 5.2.4  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> require "bit32"
> a=12
> b=9
> print(bit32.band(a,b))
8
> print(bit32.bor(a,b))
13
> print(bit32.bxor(a,b))
5
> print(bit32.bnot(a))
4294967283
> print(bit32.rshift(a,2))
3
> print(bit32.lshift(b,3))
72
> =bit32.lshift(b,3)
72

5.3 使用內建操作符

Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
> a=12
> b=9
> a & b
8
> a | b
13
> a ~ b
5
> ~ a
-13
> a >> 2
3
> b << 3
72

相關文章