lua-users home
lua-l archive

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


On Jul 10, 2013, at 5:14 AM, Philipp Janda <siffiejoe@gmx.net> wrote:

> `^` is only taken for numbers, but IMHO it doesn't make much sense to define bit operations on numbers anyway: "Let's take the number of apples and xor it with the number of oranges, right shift by the number of baskets, and we get: total nonsense"! Usually one does arithmetic _xor_ bit manipulation at any given time[*].

Besides the hash table exception you noted, there are also encryption, digital signatures, and hash (message-digest) applications.

The applications for bit fields that I've used most often in Lua, though, are communication protocols and access to hardware registers in embedded applications. There xor is used to invert bits quite often.

> So my suggestion would be: Add a bunch of operators (C-like; if you are going to write C code in Lua, it should look familiar to C programmers; also: most obvious candidates for keyword operators are already taken by bit32) and metamethods, but don't define those for any builtin datatypes. Instead provide a library that implements bitstrings as userdata using those new metamethods. Added bonus: you can define bitstrings of arbitrary length, independent of the size of the integer datatype.

As long as Lua also provided the means to efficiently convert from integer to/from bitstrings, there would be no loss for applications I mentioned above. However, there would also be no gain unless many operators (such as arithmetic comparisons) and functions were extended to work on these bitstrings.

I would be happy with bitwise and shift (and rotate) operations on the 64-bit integer types.

e