[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: boolean operators
- From: Doug Rogers <rogers@...>
- Date: Wed, 20 Sep 2006 17:40:24 -0400
Roberto Ierusalimschy wrote:
> May I ask you (and others) why you need boolean operators in Lua? (This
> is not a rhetorical question. I really want to know what uses boolean
> operators may have in Lua.)
At first I was writing a response about how 'and', 'or', and 'not' do
not behave like '&&', '||', and '!', and how I didn't like the way Lua
mixed the logical operators with numeric types. But now I see you mean
That is a subject near to my heart. I've gotta say that I have spent
LOTS of time writing bitwise stuff for Lua. It would have been a lot
easier if bitwise operators were available.
In my ideal world, 'and', 'or', 'not', ... and 'xor' ... would become
bitwise operators on numeric types. This is the path that Ada chose when
moving from Ada 83 to Ada 95. But Ada was not encumbered by the
historical precedent of sloppy typing for logical operators - the use of
'x = x or 1' when x is nil (or even false in the current release!). Ada
also had the advantage of integer types (and in this case, they also
added modular integer types).
Many things are possible, of course, each with its own trade-off. There
was a patch to add 'band', 'bor', 'bnot', and 'bxor' as infix operators
(and 'shr' and 'shl'). And there's Reuben's bitlib for a more procedural
approach. And now you mention the C-like operators '&', '|', '~', '<<'
for shift?, and what for xor, not '^'?
Eventually I settled on a more general bit string concept, which has
been very, very powerful for me. A bstring has data bytes behind it that
hold the actual bits compactly. These can be moved around in Lua as
strings. Words (numbers) can be converted into and out of bstrings. The
bstrings can be or'd, xor'd, CRC'd, FEC'd, etc. The bstring package
provides a find() function that can search for sync patterns with bit
errors. I use this package quite often to test/debug my C/C++/Ada code
because I can manipulate the items in the interpreter.
But those are large collections of bits, not mapped directly to
luaNumber, which is probably what you're after.
As you can see from the replies, we all roll our own when it comes to
extracting bit fields. This makes each application a specialty item.
Those of us who twiddle bits for a living and have found joy in Lua have
also found our own peace with the fact that Lua will be what the
community as a whole finds useful, and at the moment we're too much a
But to your question... why do we need these operators? For the same
reason we need many of the other things that Lua provides: convenience.
Doug Rogers - ICI - V:703.893.2007x220 www.innocon.com