| View previous topic :: View next topic |
| Author |
Message |
modsyn
Joined: 27 Sep 2005 Posts: 28
|
Posted: Tue Jan 24, 2006 2:44 am Post subject: Request - add bit shifting |
|
|
i recently found out that lua doesn't support shifting bits (ie, <</>>/>>>)
and i found this page with a suggested way of adding the feature:
http://lua-users.org/lists/lua-l/1999-02/msg00024.html
| For those too lazy to follow the link wrote: |
>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 |
could this possibly be added for future releases?
sorry if i posted this in the wrong place,
modsyn |
|
| Back to top |
|
 |
fullerlee
Joined: 03 Nov 2005 Posts: 54
|
Posted: Tue Jan 24, 2006 4:00 am Post subject: |
|
|
I second this request, I have a need for it in my current project.
Thanks,
Lee |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Tue Jan 24, 2006 7:28 am Post subject: |
|
|
| What about x << y == x * math.pow(2, y) and x >> y == math.floor(x / math.pow(2, y)) ? |
|
| Back to top |
|
 |
fullerlee
Joined: 03 Nov 2005 Posts: 54
|
Posted: Tue Jan 24, 2006 7:50 am Post subject: |
|
|
Shine,
I have used mathematics in place of bit shifting, I just thought geniune bit shifting would give some decent performance benefits.
Thanks,
Lee |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Tue Jan 24, 2006 8:03 am Post subject: |
|
|
| Do you need the speed? Lua uses always floats as its numbers, so perhaps it would be a better idea to extend the PRX concept of Lua Player and write performance critical parts in your own PRX in C. |
|
| Back to top |
|
 |
fullerlee
Joined: 03 Nov 2005 Posts: 54
|
Posted: Tue Jan 24, 2006 11:55 am Post subject: |
|
|
Time will tell regarding performance. My project involves an AI player and a lot of tree searching, at the moment, performance looks like it might be acceptable, but I've been keeping an eye on the PRX concept.
Keep up the good work!
Thanks,
Lee |
|
| Back to top |
|
 |
|