Browse Source

use of 'conventional' names for shift and rotate operations +

right/left versions for them
pull/9/head
Roberto Ierusalimschy 15 years ago
parent
commit
af512ad6da
  1. 34
      lbitlib.c

34
lbitlib.c

@ -1,5 +1,5 @@
/* /*
** $Id: lbitlib.c,v 1.2 2009/11/24 12:05:44 roberto Exp roberto $ ** $Id: lbitlib.c,v 1.3 2010/01/12 19:40:02 roberto Exp roberto $
** Standard library for bitwise operations ** Standard library for bitwise operations
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -80,9 +80,8 @@ static int b_not (lua_State *L) {
} }
static int b_shift (lua_State *L) { static int b_shift (lua_State *L, int i) {
b_uint r = getuintarg(L, 1); b_uint r = getuintarg(L, 1);
lua_Integer i = luaL_checkinteger(L, 2);
if (i < 0) { /* shift right? */ if (i < 0) { /* shift right? */
i = -i; i = -i;
if (i >= NBITS) r = 0; if (i >= NBITS) r = 0;
@ -97,9 +96,18 @@ static int b_shift (lua_State *L) {
} }
static int b_rotate (lua_State *L) { static int b_lshift (lua_State *L) {
return b_shift(L, luaL_checkint(L, 2));
}
static int b_rshift (lua_State *L) {
return b_shift(L, -luaL_checkint(L, 2));
}
static int b_rot (lua_State *L, int i) {
b_uint r = getuintarg(L, 1); b_uint r = getuintarg(L, 1);
lua_Integer i = luaL_checkinteger(L, 2);
i &= (NBITS - 1); /* i = i % NBITS */ i &= (NBITS - 1); /* i = i % NBITS */
r = (r << i) | (r >> (NBITS - i)); r = (r << i) | (r >> (NBITS - i));
lua_pushnumber(L, lua_uint2number(r)); lua_pushnumber(L, lua_uint2number(r));
@ -107,14 +115,26 @@ static int b_rotate (lua_State *L) {
} }
static int b_rol (lua_State *L) {
return b_rot(L, luaL_checkint(L, 2));
}
static int b_ror (lua_State *L) {
return b_rot(L, -luaL_checkint(L, 2));
}
static const luaL_Reg bitlib[] = { static const luaL_Reg bitlib[] = {
{"band", b_and}, {"band", b_and},
{"btest", b_test}, {"btest", b_test},
{"bor", b_or}, {"bor", b_or},
{"bxor", b_xor}, {"bxor", b_xor},
{"bnot", b_not}, {"bnot", b_not},
{"bshift", b_shift}, {"lshift", b_lshift},
{"brotate", b_rotate}, {"rshift", b_rshift},
{"rol", b_rol},
{"ror", b_ror},
{NULL, NULL} {NULL, NULL}
}; };

Loading…
Cancel
Save