bitwise.lua

main 39 lines · 750 bytes Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1Bit = {}
2
3function Bit.bit(p)
4 return 2 ^ p
5end
6
7function Bit.hasBit(x, p)
8 return x % (p + p) >= p
9end
10
11function Bit.setbit(x, p)
12 return Bit.hasBit(x, p) and x or x + p
13end
14
15function Bit.clearbit(x, p)
16 return Bit.hasBit(x, p) and x - p or x
17end
18
19if not _G.bit32 then
20 local bit = _G.bit or _G.require and _G.require('bit') or nil
21 if bit then
22 _G.bit32 = {
23 band = bit.band,
24 bor = bit.bor,
25 bxor = bit.bxor,
26 bnot = bit.bnot,
27 lshift = bit.lshift,
28 rshift = bit.rshift,
29 extract = bit.extract,
30 replace = bit.replace,
31 }
32 else
33 _G.bit32 = {
34 band = function(a, b) return a % (2^32) end,
35 bor = function(a, b) return a end,
36 bxor = function(a, b) return 0 end,
37 }
38 end
39end
Top