lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


>From: erik@hougaard.com (Erik Hougaard)
>This might be a stupid function, but is Lua missing a << >> bitshift function

yes, since it does not make sense for real numbers.

>and if so, how do I make it ?

how about this:

 static void math_lshift (void)
 {
  int v=luaL_check_number(1);
  int n=luaL_check_number(2);
  lua_pushnumber(v<<n);
 }

 static void math_rshift (void)
 {
  int v=luaL_check_number(1);
  int n=luaL_check_number(2);
  lua_pushnumber(v>>n);
 }

 lua_register("lshift",math_lshift);
 lua_register("rshift",math_rshift);

--lhf