Debian Codegraph @

extensions/Scribunto/includes/Engines/LuaCommon/lualib/luabit/bit.lua

raw symbols SCIP
1 --[[---------------
2 LuaBit v0.4
3 -------------------
4 a bitwise operation lib for lua.
5
6 http://luaforge.net/projects/bit/
7
8 How to use:
9 -------------------
10 bit.bnot(n) -- bitwise not (~n)
11 bit.band(m, n) -- bitwise and (m & n)
12 bit.bor(m, n) -- bitwise or (m | n)
13 bit.bxor(m, n) -- bitwise xor (m ^ n)
14 bit.brshift(n, bits) -- right shift (n >> bits)
15 bit.blshift(n, bits) -- left shift (n << bits)
16 bit.blogic_rshift(n, bits) -- logic right shift(zero fill >>>)
17
18 Please note that bit.brshift and bit.blshift only support number within
19 32 bits.
20
21 2 utility functions are provided too:
22 bit.tobits(n) -- convert n into a bit table(which is a 1/0 sequence)
23 -- high bits first
24 bit.tonumb(bit_tbl) -- convert a bit table into a number
25 -------------------
26
27 Under the MIT license.
28
29 copyright(c) 2006~2007 hanzhao ([email protected])
30
31 2013-02-20: Brad Jorsch: Fix to not try messing with globals, doesn't work in Scribunto
32 --]]---------------
33
34 do
35
36 ------------------------
37 -- bit lib implementions
38
39 local function check_int(n)
40 -- checking not float
41 if(n - math.floor(n) > 0) then
42 error("trying to use bitwise operation on non-integer!")
43 end
44 end
45
46 local function to_bits(n)
47 check_int(n)
48 if(n < 0) then
49 -- negative
50 return to_bits(bit.bnot(math.abs(n)) + 1)
51 end
52 -- to bits table
53 local tbl = {}
54 local cnt = 1
55 while (n > 0) do
56 local last = math.mod(n,2)
57 if(last == 1) then
58 tbl[cnt] = 1
59 else
60 tbl[cnt] = 0
61 end
62 n = (n-last)/2
63 cnt = cnt + 1
64 end
65
66 return tbl
67 end
68
69 local function tbl_to_number(tbl)
70 local n = table.getn(tbl)
71
72 local rslt = 0
73 local power = 1
74 for i = 1, n do
75 rslt = rslt + tbl[i]*power
76 power = power*2
77 end
78
79 return rslt
80 end
81
82 local function expand(tbl_m, tbl_n)
83 local big = {}
84 local small = {}
85 if(table.getn(tbl_m) > table.getn(tbl_n)) then
86 big = tbl_m
87 small = tbl_n
88 else
89 big = tbl_n
90 small = tbl_m
91 end
92 -- expand small
93 for i = table.getn(small) + 1, table.getn(big) do
94 small[i] = 0
95 end
96
97 end
98
99 local function bit_or(m, n)
100 local tbl_m = to_bits(m)
101 local tbl_n = to_bits(n)
102 expand(tbl_m, tbl_n)
103
104 local tbl = {}
105 local rslt = math.max(table.getn(tbl_m), table.getn(tbl_n))
106 for i = 1, rslt do
107 if(tbl_m[i]== 0 and tbl_n[i] == 0) then
108 tbl[i] = 0
109 else
110 tbl[i] = 1
111 end
112 end
113
114 return tbl_to_number(tbl)
115 end
116
117 local function bit_and(m, n)
118 local tbl_m = to_bits(m)
119 local tbl_n = to_bits(n)
120 expand(tbl_m, tbl_n)
121
122 local tbl = {}
123 local rslt = math.max(table.getn(tbl_m), table.getn(tbl_n))
124 for i = 1, rslt do
125 if(tbl_m[i]== 0 or tbl_n[i] == 0) then
126 tbl[i] = 0
127 else
128 tbl[i] = 1
129 end
130 end
131
132 return tbl_to_number(tbl)
133 end
134
135 local function bit_not(n)
136 local tbl = to_bits(n)
137 local size = math.max(table.getn(tbl), 32)
138 for i = 1, size do
139 if(tbl[i] == 1) then
140 tbl[i] = 0
141 else
142 tbl[i] = 1
143 end
144 end
145 return tbl_to_number(tbl)
146 end
147
148 local function bit_xor(m, n)
149 local tbl_m = to_bits(m)
150 local tbl_n = to_bits(n)
151 expand(tbl_m, tbl_n)
152
153 local tbl = {}
154 local rslt = math.max(table.getn(tbl_m), table.getn(tbl_n))
155 for i = 1, rslt do
156 if(tbl_m[i] ~= tbl_n[i]) then
157 tbl[i] = 1
158 else
159 tbl[i] = 0
160 end
161 end
162
163 --table.foreach(tbl, print)
164
165 return tbl_to_number(tbl)
166 end
167
168 local function bit_rshift(n, bits)
169 check_int(n)
170
171 local high_bit = 0
172 if(n < 0) then
173 -- negative
174 n = bit_not(math.abs(n)) + 1
175 high_bit = 2147483648 -- 0x80000000
176 end
177
178 for i=1, bits do
179 n = n/2
180 n = bit_or(math.floor(n), high_bit)
181 end
182 return math.floor(n)
183 end
184
185 -- logic rightshift assures zero filling shift
186 local function bit_logic_rshift(n, bits)
187 check_int(n)
188 if(n < 0) then
189 -- negative
190 n = bit_not(math.abs(n)) + 1
191 end
192 for i=1, bits do
193 n = n/2
194 end
195 return math.floor(n)
196 end
197
198 local function bit_lshift(n, bits)
199 check_int(n)
200
201 if(n < 0) then
202 -- negative
203 n = bit_not(math.abs(n)) + 1
204 end
205
206 for i=1, bits do
207 n = n*2
208 end
209 return bit_and(n, 4294967295) -- 0xFFFFFFFF
210 end
211
212 local function bit_xor2(m, n)
213 local rhs = bit_or(bit_not(m), bit_not(n))
214 local lhs = bit_or(m, n)
215 return bit_and(lhs, rhs)
216 end
217
218 --------------------
219 -- bit lib interface
220
221 local bit = {
222 -- bit operations
223 bnot = bit_not,
224 band = bit_and,
225 bor = bit_or,
226 bxor = bit_xor,
227 brshift = bit_rshift,
228 blshift = bit_lshift,
229 bxor2 = bit_xor2,
230 blogic_rshift = bit_logic_rshift,
231
232 -- utility func
233 tobits = to_bits,
234 tonumb = tbl_to_number,
235 }
236
237 return bit
238
239 end
240
241 --[[
242 for i = 1, 100 do
243 for j = 1, 100 do
244 if(bit.bxor(i, j) ~= bit.bxor2(i, j)) then
245 error("bit.xor failed.")
246 end
247 end
248 end
249 --]]