diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-02-11 13:55:29 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-02-11 13:55:29 -0200 |
commit | af512ad6da83f75b9e764ceeba28198e6e842377 (patch) | |
tree | 9f217a4d9cb12adb9cebef71d541ed759d48c80a /lbitlib.c | |
parent | d27108ccd543049bd2006aa16ed74f8c4d899731 (diff) | |
download | lua-github-af512ad6da83f75b9e764ceeba28198e6e842377.tar.gz |
use of 'conventional' names for shift and rotate operations +
right/left versions for them
Diffstat (limited to 'lbitlib.c')
-rw-r--r-- | lbitlib.c | 34 |
1 files changed, 27 insertions, 7 deletions
@@ -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} }; |