lua-users home
lua-l archive

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


> I know this topic comes up every now and then,
> but I still haven't seen a succinct summary of
> it -- but basically fast bit flags don't seem
> possible with Lua, which somewhat limits it
> as a scripting language for a project I'm
> working on.

It shouldn't limit you at all.

Yes, doing bit manipulation in Lua is painfully slow.  You can speed it up
by using tables that precompute the values of the operations you care about.
For example, here's a table that lets you AND two nybbles:

nybbleAnd = {
    [0] = {[0]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    [1] = {[0]=0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
    [2] = {[0]=0,0,2,2,0,0,2,2,0,0,2,2,0,0,2,2},
    [3] = {[0]=0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3},
    [4] = {[0]=0,0,0,0,4,4,4,4,0,0,0,0,4,4,4,4},
    [5] = {[0]=0,1,0,1,4,5,4,5,0,1,0,1,4,5,4,5},
    [6] = {[0]=0,0,2,2,4,4,6,6,0,0,2,2,4,4,6,6},
    [7] = {[0]=0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7},
    [8] = {[0]=0,0,0,0,0,0,0,0,8,8,8,8,8,8,8,8},
    [9] = {[0]=0,1,0,1,0,1,0,1,8,9,8,9,8,9,8,9},
    [10] = {[0]=0,0,2,2,0,0,2,2,8,8,10,10,8,8,10,10},
    [11] = {[0]=0,1,2,3,0,1,2,3,8,9,10,11,8,9,10,11},
    [12] = {[0]=0,0,0,0,4,4,4,4,8,8,8,8,12,12,12,12},
    [13] = {[0]=0,1,0,1,4,5,4,5,8,9,8,9,12,13,12,13},
    [14] = {[0]=0,0,2,2,4,4,6,6,8,8,10,10,12,12,14,14},
    [15] = {[0]=0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
}

Of course, this is insane.

The answer here is the punch line to the old joke "Doctor, it hurts when I
do this."  If something is painful to do in Lua, then don't do it in Lua!
Extend Lua with the functionality you want-- it's easy, and it's fun.  For
bit manipulation, you can extend Lua with the bitlib library that I
originally wrote and Reuben Thomas nicely improved.  I Googled and found a
link at http://www.mupsych.org/~rrt/Lua

I have mixed feelings about Lua not including such a library natively.  On
the one hand, bit manipulation is such a common operation that it should be
part of Lua.  On the other, part of the appeal of Lua is that the language
doesn't have a lot of fluff.  Lua is (to me at least) best thought of as a
framework for creating application-specific languages.  It provides the bare
essentials, and you build up from that.  It's the same kind mindset behind
languages like Forth, where the core language doesn't provide everything
"out of the box," but does provide what you need to extend the language as
necessary for your application.

> The key issue is that Lua doesn't have an integer
> type and the associate bit twiddling operators.
> I'm assuming that this has been shot down numerous
> times as bad, so what are people doing in the
> interim to address this?

As the bitlib shows, you don't need an integer type in Lua to do bit
manipulation.  It might have been more efficient if Lua provided an integer
type, but that would add complexity to the language that doesn't need to be
there.