2
2
3
3
LUA MODULE
4
4
5
- bit.numberlua - Bitwise operations implemented in pure Lua as numbers.
5
+ bit.numberlua - Bitwise operations implemented in pure Lua as numbers,
6
+ with Lua 5.2 'bit32' and (LuaJIT) LuaBitOp 'bit' compatibility interfaces.
6
7
7
8
SYNOPSIS
8
9
@@ -13,6 +14,10 @@ SYNOPSIS
13
14
local bit32 = require 'bit.numberlua'.bit32
14
15
assert(bit32.band(-1) == 0xffffffff)
15
16
17
+ -- Interface providing strong (LuaJIT) LuaBitOp 'bit' compatibility
18
+ local bit = require 'bit.numberlua'.bit
19
+ assert(bit.tobit(0xffffffff) == -1)
20
+
16
21
DESCRIPTION
17
22
18
23
This library implements bitwise operations entirely in Lua.
@@ -33,10 +38,11 @@ DESCRIPTION
33
38
The `xor` function in this module is based partly on Roberto Ierusalimschy's
34
39
post in http://lua-users.org/lists/lua-l/2002-09/msg00134.html .
35
40
36
- The included BIT.bit32 sublibrary aims to provide 100% compatibility with
37
- the Lua 5.2 "bit32" library. This compatbility is at the cost of some
38
- efficiency since inputted numbers are normalized and more general
39
- forms (e.g. multi-argument bitwise operators) are supported.
41
+ The included BIT.bit32 and BIT.bit sublibraries aims to provide 100%
42
+ compatibility with the Lua 5.2 "bit32" and (LuaJIT) LuaBitOp "bit" library.
43
+ This compatbility is at the cost of some efficiency since inputted
44
+ numbers are normalized and more general forms (e.g. multi-argument
45
+ bitwise operators) are supported.
40
46
41
47
STATUS
42
48
48
54
resolve these differences.
49
55
50
56
The BIT.bit32 library passes the Lua 5.2 test suite (bitwise.lua)
51
- http://www.lua.org/tests/5.2/ .
57
+ http://www.lua.org/tests/5.2/ . The BIT.bit library passes the LuaBitOp
58
+ test suite (bittest.lua). However, these have not been tested on
59
+ platforms with Lua compiled with 32-bit integer numbers.
52
60
53
61
API
54
62
131
139
bit32.lshift (x, disp) --> z
132
140
bit32.rrotate (x, disp) --> z
133
141
bit32.rshift (x, disp) --> z
142
+
143
+ BIT.bit
144
+
145
+ This table contains functions that aim to provide 100% compatibility
146
+ with the LuaBitOp "bit" library (from LuaJIT).
147
+
148
+ bit.tobit(x) --> y
149
+ bit.tohex(x [,n]) --> y
150
+ bit.bnot(x) --> y
151
+ bit.bor(x1 [,x2...]) --> y
152
+ bit.band(x1 [,x2...]) --> y
153
+ bit.bxor(x1 [,x2...]) --> y
154
+ bit.lshift(x, n) --> y
155
+ bit.rshift(x, n) --> y
156
+ bit.arshift(x, n) --> y
157
+ bit.rol(x, n) --> y
158
+ bit.ror(x, n) --> y
159
+ bit.bswap(x) --> y
134
160
135
161
DEPENDENCIES
136
162
@@ -248,13 +274,15 @@ lshift = M.lshift
248
274
function M .tohex (x , n ) -- BitOp style
249
275
n = n or 8
250
276
local up
251
- if n < 0 then
277
+ if n <= 0 then
278
+ if n == 0 then return ' ' end
252
279
up = true
253
280
n = - n
254
281
end
255
282
x = band (x , 16 ^ n - 1 )
256
283
return (' %0' .. n .. (up and ' X' or ' x' )):format (x )
257
284
end
285
+ local tohex = M .tohex
258
286
259
287
function M .extract (n , field , width ) -- Lua5.2 inspired
260
288
width = width or 1
@@ -278,6 +306,7 @@ function M.bswap(x) -- BitOp style
278
306
local d = band (x , 0xff )
279
307
return lshift (lshift (lshift (a , 8 ) + b , 8 ) + c , 8 ) + d
280
308
end
309
+ local bswap = M .bswap
281
310
282
311
function M .rrotate (x , disp ) -- Lua5.2 inspired
283
312
disp = disp % 32
@@ -300,6 +329,7 @@ function M.arshift(x, disp) -- Lua5.2 inspired
300
329
if x >= 0x80000000 then z = z + lshift (2 ^ disp - 1 , 32 - disp ) end
301
330
return z
302
331
end
332
+ local arshift = M .arshift
303
333
304
334
function M .btest (x , y ) -- Lua5.2 inspired
305
335
return band (x , y ) ~= 0
@@ -424,8 +454,82 @@ function M.bit32.replace(x, v, field, ...)
424
454
end
425
455
426
456
427
- -- TODO? Likewise add LuaOp "bit" compat section?
428
- -- M.bit32 = {} -- LuaOp "bit" compatibility
457
+ --
458
+ -- Start LuaBitOp "bit" compat section.
459
+ --
460
+
461
+ M .bit = {} -- LuaBitOp "bit" compatibility
462
+
463
+ function M .bit .tobit (x )
464
+ x = x % MOD
465
+ if x >= 0x80000000 then x = x - MOD end
466
+ return x
467
+ end
468
+ local bit_tobit = M .bit .tobit
469
+
470
+ function M .bit .tohex (x , ...)
471
+ return tohex (x % MOD , ... )
472
+ end
473
+
474
+ function M .bit .bnot (x )
475
+ return bit_tobit (bnot (x % MOD ))
476
+ end
477
+
478
+ local function bit_bor (a , b , c , ...)
479
+ if c then
480
+ return bit_bor (bit_bor (a , b ), c , ... )
481
+ elseif b then
482
+ return bit_tobit (bor (a % MOD , b % MOD ))
483
+ else
484
+ return bit_tobit (a )
485
+ end
486
+ end
487
+ M .bit .bor = bit_bor
488
+
489
+ local function bit_band (a , b , c , ...)
490
+ if c then
491
+ return bit_band (bit_band (a , b ), c , ... )
492
+ elseif b then
493
+ return bit_tobit (band (a % MOD , b % MOD ))
494
+ else
495
+ return bit_tobit (a )
496
+ end
497
+ end
498
+ M .bit .band = bit_band
499
+
500
+ local function bit_bxor (a , b , c , ...)
501
+ if c then
502
+ return bit_bxor (bit_bxor (a , b ), c , ... )
503
+ elseif b then
504
+ return bit_tobit (bxor (a % MOD , b % MOD ))
505
+ else
506
+ return bit_tobit (a )
507
+ end
508
+ end
509
+ M .bit .bxor = bit_bxor
510
+
511
+ function M .bit .lshift (x , n )
512
+ return bit_tobit (lshift (x % MOD , n % 32 ))
513
+ end
429
514
515
+ function M .bit .rshift (x , n )
516
+ return bit_tobit (rshift (x % MOD , n % 32 ))
517
+ end
518
+
519
+ function M .bit .arshift (x , n )
520
+ return bit_tobit (arshift (x % MOD , n % 32 ))
521
+ end
522
+
523
+ function M .bit .rol (x , n )
524
+ return bit_tobit (lrotate (x % MOD , n % 32 ))
525
+ end
526
+
527
+ function M .bit .ror (x , n )
528
+ return bit_tobit (rrotate (x % MOD , n % 32 ))
529
+ end
530
+
531
+ function M .bit .bswap (x )
532
+ return bit_tobit (bswap (x % MOD ))
533
+ end
430
534
431
535
return M
0 commit comments