-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinops.lua
54 lines (50 loc) · 1.18 KB
/
binops.lua
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
--[[pod_format="raw",created="2024-03-19 22:36:09",modified="2024-03-19 22:42:00",revision=6]]
-- despite this polyfill, things will still be awkward
-- because p8 uses 16.16 fixed-point
-- but p64 uses floating point
-- cart authors will need to manually fix this
function band(x,y)
if (type(x) != "number") x = 0
if (type(y) != "number") y = 0
return x&y
end
function bor(x,y)
if (type(x) != "number") x = 0
if (type(y) != "number") y = 0
return x|y
end
function bxor(x,y)
if (type(x) != "number") x = 0
if (type(y) != "number") y = 0
return x^^y
end
function bnot(x)
if (type(x) != "number") x = 0
return ~x
end
function shl(x,n)
if (type(x) != "number") x = 0
if (type(n) != "number") n = 0
return x<<n
end
function shr(x,n)
if (type(x) != "number") x = 0
if (type(n) != "number") n = 0
return x>>n
end
-- these operators don't exist (yet?)
--function lshr(x,n)
-- if (type(x) != "number") x = 0
-- if (type(n) != "number") n = 0
-- return x>>>n
--end
--function rotl(x,n)
-- if (type(x) != "number") x = 0
-- if (type(n) != "number") n = 0
-- return x<<>n
--end
--function rotr(x,n)
-- if (type(x) != "number") x = 0
-- if (type(n) != "number") n = 0
-- return x>><n
--end