From af512ad6da83f75b9e764ceeba28198e6e842377 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 11 Feb 2010 13:55:29 -0200 Subject: [PATCH] use of 'conventional' names for shift and rotate operations + right/left versions for them --- lbitlib.c | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/lbitlib.c b/lbitlib.c index 57a3a60f..3b5f5b83 100644 --- a/lbitlib.c +++ b/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 ** 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); - lua_Integer i = luaL_checkinteger(L, 2); if (i < 0) { /* shift right? */ i = -i; 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); - lua_Integer i = luaL_checkinteger(L, 2); i &= (NBITS - 1); /* i = i % NBITS */ r = (r << i) | (r >> (NBITS - i)); 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[] = { {"band", b_and}, {"btest", b_test}, {"bor", b_or}, {"bxor", b_xor}, {"bnot", b_not}, - {"bshift", b_shift}, - {"brotate", b_rotate}, + {"lshift", b_lshift}, + {"rshift", b_rshift}, + {"rol", b_rol}, + {"ror", b_ror}, {NULL, NULL} };