lua-users home
lua-l archive

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


On Wed, Dec 17, 2008 at 10:12 AM, Mike Pall wrote:
> Well, I guess this is close to the least-efficient way to do bit
> ops in pure Lua. ;-)

Or least in-efficient?  That is, representing bitarrays in Lua as
numbers of the form SUM a_j * 16^j.  The other pure Lua approaches
I've seen represent them as tables like {0,1,0,1,0,1,0,1,.....} or as
strings like "01010101.....".  Both of these might be similarly
improved though using nibbles like {5,5,0,0} and "\5\5\0\0" with 4-bit
x 4-bit lookup tables:

  and16 = {.....}  -- array of 16x16 lookup values
  ...
  local a1,a2,a3,a4 = string.byte(a, 1,4);
  local b1,b2,b3,b4 = string.byte(b, 1,4);
  c = string.char(and16[a1 + 16 * b1], and16[a2 + 16 * b2],
                        and16[a3 + 16 * b3], and16[a4 + 16 * b4]