forked from yuin/gopher-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit32lib.go
64 lines (55 loc) · 1 KB
/
bit32lib.go
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
package lua
func OpenBit32(L *LState) int {
mod := L.RegisterModule(Bit32LibName, bit32Funcs)
L.Push(mod)
return 1
}
var bit32Funcs = map[string]LGFunction{
"band": bandFn,
"bnot": bnotFn,
"bor": borFn,
"bxor": bxorFn,
"lshift": lshiftFn,
"rshift": rshiftFn,
}
func bandFn(L *LState) int {
a := L.CheckInt(1)
b := L.CheckInt(2)
result := a & b
L.Push(LNumber(result))
return 1
}
func bnotFn(L *LState) int {
a := L.CheckInt(1)
result := ^a
L.Push(LNumber(result))
return 1
}
func borFn(L *LState) int {
a := L.CheckInt(1)
b := L.CheckInt(2)
result := a | b
L.Push(LNumber(result))
return 1
}
func bxorFn(L *LState) int {
a := L.CheckInt(1)
b := L.CheckInt(2)
result := a ^ b
L.Push(LNumber(result))
return 1
}
func lshiftFn(L *LState) int {
a := L.CheckInt(1)
b := L.CheckInt(2)
result := a << uint(b)
L.Push(LNumber(result))
return 1
}
func rshiftFn(L *LState) int {
n := L.CheckInt64(1)
n2 := L.CheckInt(2)
result := n >> uint8(n2)
L.Push(LNumber(result))
return 1
}