Request - add bit shifting

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
modsyn
Posts: 28
Joined: Tue Sep 27, 2005 6:02 am

Request - add bit shifting

Post by modsyn »

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: [email protected] (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
fullerlee
Posts: 54
Joined: Thu Nov 03, 2005 9:46 am

Post by fullerlee »

I second this request, I have a need for it in my current project.

Thanks,

Lee
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

What about x << y == x * math.pow(2, y) and x >> y == math.floor(x / math.pow(2, y)) ?
fullerlee
Posts: 54
Joined: Thu Nov 03, 2005 9:46 am

Post by fullerlee »

Shine,

I have used mathematics in place of bit shifting, I just thought geniune bit shifting would give some decent performance benefits.

Thanks,

Lee
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

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.
fullerlee
Posts: 54
Joined: Thu Nov 03, 2005 9:46 am

Post by fullerlee »

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
Post Reply