lua-users home
lua-l archive

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


2007/3/13, Peter Odding <xolox@home.nl>:
*Bitfields*

As Alex and others mentions, strings are clearer to use. You can use
simple arrays, with the presence of the string meaning the bit is on
and its absence means the bit is off. You can also use Lua sets, that
is a table with the string as keys and a boolean as value. That way
it's easy to check for the presence of a bit.

For me string tables have two big advantages. First they have no limit
on the number of flags you can put in it, whereas an integer bitfield
is limited to 32 or 64 flags. Second they are much higher level. That
means that it will make the code clearer (and less likely to have
bugs), and more accessible to newbies (in a non-pejorative sense, be
it junior programmers, scripters, non-technical users).

The sets are easier to query, and should be used as output. The arrays
are easier to build, and should be accepted as input. A good rule is
to be tolerant on input you accept, and strict on output you provide.
A set like { 'alpha', 'beta', gamma=true, delta=false } would then
mean that alpha, beta and gamma are on, and delta and any other bit
are off.

*Microseconds*

Since numbers in Lua are floating point you should use plain units
without multiplier, that is seconds in that case.