-
Notifications
You must be signed in to change notification settings - Fork 2
/
bit.h
71 lines (61 loc) · 1.54 KB
/
bit.h
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
#include "lib.h"
static int bit_band(lua_State *L) {
unsigned int a = lua_tointeger(L, 1);
unsigned int b = lua_tointeger(L, 2);
lua_pushinteger(L, a & b);
return 1;
}
static int bit_bor(lua_State *L) {
unsigned int a = lua_tointeger(L, 1);
unsigned int b = lua_tointeger(L, 2);
lua_pushinteger(L, a | b);
return 1;
}
static int bit_bnot(lua_State *L) {
unsigned int a = lua_tointeger(L, 1);
lua_pushinteger(L, ~a & 0xFFFFFFFF);
return 1;
}
static int bit_bxor(lua_State *L) {
unsigned int a = lua_tointeger(L, 1);
unsigned int b = lua_tointeger(L, 2);
lua_pushinteger(L, a ^ b);
return 1;
}
static int bit_blshift(lua_State *L) {
unsigned int a = lua_tointeger(L, 1);
unsigned int b = lua_tointeger(L, 2);
lua_pushinteger(L, a << b);
return 1;
}
static int bit_brshift(lua_State *L) {
unsigned int a = lua_tointeger(L, 1);
unsigned int b = lua_tointeger(L, 2);
lua_pushinteger(L, a >> b | ((((a & 0x80000000) << b) - 1) << (32 - b)));
return 1;
}
static int bit_blogic_rshift(lua_State *L) {
unsigned int a = lua_tointeger(L, 1);
unsigned int b = lua_tointeger(L, 2);
lua_pushinteger(L, a >> b);
return 1;
}
static const char * bit_keys[7] = {
"band",
"bor",
"bnot",
"bxor",
"blshift",
"brshift",
"blogic_rshift"
};
static lua_CFunction bit_values[7] = {
bit_band,
bit_bor,
bit_bnot,
bit_bxor,
bit_blshift,
bit_brshift,
bit_blogic_rshift
};
static library_t bit_lib = {"bit", 7, bit_keys, bit_values};