lua bitモジュール

1278 ワード

公式住所
1.呼び出し
local bit = require("bit")

2.関数
関数の定義
function printx(x)
  print("0x"..bit.tohex(x))
end

--変換y=bit.tobit(x) y = bit.tohex(x [,n]) y = bit.bnot(x)
--と非y=bit.bor(x1 [,x2...]) y = bit.band(x1 [,x2...]) y = bit.bxor(x1 [,x2...])
--論理左シフト/論理右シフト/算術右シフトy=bit.lshift(x, n) y = bit.rshift(x, n) y = bit.arshift(x, n)
例:
print(bit.lshift(1, 0))              --> 1
print(bit.lshift(1, 8))              --> 256
print(bit.lshift(1, 40))             --> 256
print(bit.rshift(256, 8))            --> 1
print(bit.rshift(-256, 8))           --> 16777215
print(bit.arshift(256, 8))           --> 1
print(bit.arshift(-256, 8))          --> -1
printx(bit.lshift(0x87654321, 12))   --> 0x54321000
printx(bit.rshift(0x87654321, 12))   --> 0x00087654
printx(bit.arshift(0x87654321, 12))  --> 0xfff87654

--左回転/右回転
y = bit.rol(x, n) y = bit.ror(x, n)
printx(bit.rol(0x12345678, 12))   --> 0x45678123
printx(bit.ror(0x12345678, 12))   --> 0x67812345

--交換
y = bit.bswap(x)
printx(bit.bswap(0x12345678)) --> 0x78563412
printx(bit.bswap(0x78563412)) --> 0x12345678