-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbitlibemu.lua
More file actions
82 lines (67 loc) · 1.55 KB
/
bitlibemu.lua
File metadata and controls
82 lines (67 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
-------------------------------------------------
--- *** BitLibEmu for Lua *** ---
-------------------------------------------------
--- Author: Martin Huesser ---
--- Date: 2008-06-16 ---
--- License: You may use this code in your ---
--- projects as long as this header ---
--- stays intact. ---
-------------------------------------------------
local mod = math.fmod
local floor = math.floor
bit = {}
----------------------------------------
local function cap(x)
return mod(x,4294967296)
end
----------------------------------------
function bit.bnot(x)
return 4294967295-cap(x)
end
----------------------------------------
function bit.lshift(x,n)
return cap(cap(x)*2^n)
end
----------------------------------------
function bit.rshift(x,n)
return floor(cap(x)/2^n)
end
----------------------------------------
function bit.band(x,y)
local z,i,j = 0,1
for j = 0,31 do
if (mod(x,2)==1 and mod(y,2)==1) then
z = z + i
end
x = bit.rshift(x,1)
y = bit.rshift(y,1)
i = i*2
end
return z
end
----------------------------------------
function bit.bor(x,y)
local z,i,j = 0,1
for j = 0,31 do
if (mod(x,2)==1 or mod(y,2)==1) then
z = z + i
end
x = bit.rshift(x,1)
y = bit.rshift(y,1)
i = i*2
end
return z
end
----------------------------------------
function bit.bxor(x,y)
local z,i,j = 0,1
for j = 0,31 do
if (mod(x,2)~=mod(y,2)) then
z = z + i
end
x = bit.rshift(x,1)
y = bit.rshift(y,1)
i = i*2
end
return z
end