lua-users home
lua-l archive

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

On Jul 9, 2013, at 2:23 PM, Ico wrote:

> People who regularly use bit operation probably do so in a language
> where native operators are available (i.e. C), and chances are they are
> familiar with the various idioms, for example
>  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.bnot(mask))

A coincidence: "b".."and" is the English word "band", with one meaning being "a range within a bigger set". So we have the "FM radio band" of the RF spectrum, here frequencies in 87MHz-108MHz. In this message's context I read as an operator working on a band of bits within val.

Too often C bit manipulation is a fun(?) puzzle when working with hardware or protocols. There are sometimes reasons to be very precise with how hardware is manipulated, but certainly for data manipulation I would much rather think in terms of bands of data. Since I can't seem to post without some lunatic syntax:

  -- extract the high byte of the length/cmd word
  cmd = v~[31,24]
  -- clear it
  v~[31,24] = 0

And of course this already exists as 

  cmd = bit32.extract(v, 24, 8) 
  v = bit32.replace(v, 0, 24, 8)

If I'm feeling disciplined, these become higher order functions or metatable trickery; why define MASK when you can expose set_cmd and get_cmd? The v~[] syntax has the downside that there is no clear way to give symbolic names to bands except possibly functions with one or two return values.

I guess my point is that if I only have a limited amount of syntax and complexity to go around, I would not want to spend it on full-generality boolean bit operations from C, since the band operations seem at least as common. But I don't know; I usually used them curried.