lua-users home
lua-l archive

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

> May I ask you (and others) why you need boolean operators in Lua?

You haven't specified if you mean literally "operators" like >> and << in C, or simply to imbed a bitwise manipulation library in the standard distribution.

I think you can make 5 major cases for some sort of bitwise operation in the default implementation:

1. To interface with the host program, which may need you to set or test individual bits

2. To do calculations (like checksums, encryption) which are defined in terms of XOR and similar.

3. Most script languages other than Lua support bitwise operations.

4. Extending the language (by using loadlib etc.) is not always possible for the end-user.

5. It is easy to do.

Interface with host

One of the major features of Lua is its use as an imbedded script language. Thus, the needs of the host need to be taken into consideration. There are many cases where the host may need flags set or tested. For example, interfacing with a GUI operating system, you may have a bit set to indicate if the Ctrl or Shift key is pressed.

Also when calling the host program's functions you may need to set or clear individual bits.

Certainly, host program developers can extend Lua by adding their own "bit" library, but really, bit manipulation could easily be regarded as a "core need" and not require every developer to roll their own bit library.

Bitwise calculations

A while back I was planning to implement AES encryption using pure Lua, but the fact that almost every encryption and hash algorithm requires bitwise XOR, OR, and AND operations, this would have been very tedious in Lua. It is *possible*, by converting numbers to strings and back, but the results would surely be appallingly slow (and hard to read). I could have loaded in a bit library to do the bit manipulation, but if you are going to do that, you may as well load in an encryption library itself, and bypass the bit library altogether.

Other script languages

As far as I can make out, various "competing" script languages have bit manipulation supplied as standard (eg. VBscript, Perlscript, Javascript).

Extension not always possible

There are various implementations of Lua around which make it difficult or impossible for the end-user to do things like package.loadlib, for security reasons. They don't want their users to run arbitrary code. Thus the option of loading in an external bitwise library is not always available.

Easy to do

If you simply add Reuben Thomas's bit library to the standard distribution, it is only about 60 lines of code (half of which are #defines). If a Lua user is desperate for space, they can omit it in custom versions, however the default distribution can surely spare that minor extra code?


An alternative to simply incorporating the library as standard would be to actually modify the parser and virtual machine to handle bit operations inline (eg. a = b >> 4). This would add speed no doubt, but be more work overall. Personally I would be happy if the bitwise operations were available either way - library or as direct operators.

- Nick Gammon