aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lbitlib.c14
-rwxr-xr-xtests/test-bit32.lua1
2 files changed, 13 insertions, 2 deletions
diff --git a/lbitlib.c b/lbitlib.c
index 4786c0d..db2652a 100644
--- a/lbitlib.c
+++ b/lbitlib.c
@@ -19,8 +19,18 @@
19#if defined(LUA_COMPAT_BITLIB) /* { */ 19#if defined(LUA_COMPAT_BITLIB) /* { */
20 20
21 21
22#define pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n)) 22#define pushunsigned(L,n) (sizeof(lua_Integer) > 4 ? lua_pushinteger(L, (lua_Integer)(n)) : lua_pushnumber(L, (lua_Number)(n)))
23#define checkunsigned(L,i) ((lua_Unsigned)luaL_checkinteger(L,i)) 23static lua_Unsigned checkunsigned(lua_State *L, int i) {
24 if (sizeof(lua_Integer) > 4)
25 return (lua_Unsigned)luaL_checkinteger(L, i);
26 else {
27 lua_Number d = luaL_checknumber(L, i);
28 if (d < 0)
29 d = (d + 1) + (~(lua_Unsigned)0);
30 luaL_argcheck(L, d >= 0 && d <= (~(lua_Unsigned)0), i, "value out of range");
31 return (lua_Unsigned)d;
32 }
33}
24 34
25 35
26/* number of bits to consider in a number */ 36/* number of bits to consider in a number */
diff --git a/tests/test-bit32.lua b/tests/test-bit32.lua
index cc91e52..a408b7d 100755
--- a/tests/test-bit32.lua
+++ b/tests/test-bit32.lua
@@ -4,6 +4,7 @@ local bit32 = require("bit32")
4 4
5 5
6assert(bit32.bnot(0) == 2^32-1) 6assert(bit32.bnot(0) == 2^32-1)
7assert(bit32.bnot(-1) == 0)
7assert(bit32.band(1, 3, 5) == 1) 8assert(bit32.band(1, 3, 5) == 1)
8assert(bit32.bor(1, 3, 5) == 7) 9assert(bit32.bor(1, 3, 5) == 7)
9 10