lua-users home
lua-l archive

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


On 2013-07-12 11:26 AM, "John Hind" <john.hind@zen.co.uk> wrote:
>
> I've been following the discussion on the bit32 library and share the desire
> for an operator rather than a functional syntax. However I realised that 99%
> of my use of this library is for cracking and assembling bitfields for
> communications protocols and register mapping in embedded systems. It is
> straightforward to add a syntax for this in a C library by providing a
> metatable for the number type with 'index' and 'newindex' metamethods. Then:
>
> b = n[3]        -- Extract bit 3 as a boolean.
> n[23] = true    -- Set bit 23.
> n[6] = not n[6] -- Toggle bit 6.
>

This, I like. (Though I wonder if n.bit[3] would be even better.) It's a shame this can't be made to work with Lua as it's currently implemented, but maybe it could be possible in a future version.

Beyond that, the bit ranges and fields syntax starts to look pretty hairy.

I also do like the idea of a simple way to describe bitfield structures, but I don't think it needs to be built into the language; a module should be enough. (Also, "bitfield structure" sounds a lot like "struct", and now we're moving beyond the topic of bit twiddling...)