
Am 09.07.2013 23:54 schröbte Lorenzo Donati:
I must agree with you. Although I don't need bitops so much, I find them useful from time to time. What I really hate is functional notation for them: any time you try to do littlemorethantrivial stuff you end up with ugly code (from a readability POV). Infix notation is really a must for writing readable, straightforward code using bitops IMHO. I'm not particularly fond of Clike operators, so I wouldn't object to a more luaish keywordsbased approach. Moreover you cannot have exactly the same operators as in C, since ^ is already taken,
`^` 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[*]. So my suggestion would be: Add a bunch of operators (Clike; 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.
[*]: The one exception I can think of is implementing hash tables, where most algorithms do extensive bit fiddling to generate "unexpected" bit patterns and the result is a bucket index (i.e. a genuine number). Fortunately we already have those in Lua ...
> On 09/07/2013 20.23, Ico wrote: >> >> val = val & ~mask >> >> for clearing bits in a word. Doing this with bit32 does not result in >> code that is more readable, IMHO: >> >> val = bit32.band(val, bit32.bnot(mask)) >>I usually use `val = val  mask` for this, meaning "clear all bits set in mask from val". I also use `a + b` for `a  b`, and `a( b )` for `!(b & ~a)` (i.e.: check if the set of onebits in b is a subset of onebits in a). Together with `==` this is enough to implement the common bit flags in C APIs.
Philipp