lua-users home
lua-l archive

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


Thank you for the answer.
Now another point about the bit32 library. Several years ago I write
my own "bit" library, before understanding that several versions had
already been implemented :~)
While comparing the features of my bit library and the new standard
bit32 one, I noticed that IMHO two functions are missing in the
standard version.
I call them get and set. We used them quite a lot to deal with bit
fields from hardware registers. This is how they could be defined:

bit32.get(x, disp [, nbbits]) : returns the bit slice taken from
number 'x', starting at bit # 'disp', and of size 'nbbits' bits (1 by
default).
Examples:
  assert(bit32.get(0x12345678, 4, 4) == 0x7)
  assert(bit32.get(0x12345678, 16, 8) == 0x34)
  assert(bit32.get(0x12345678, 3) == 1)

bit32.set(val, x, disp [, nbbits]) : inserts the value 'val' as a bit
slice into number 'x', starting at bit position 'disp', and of size
'nbbits' bits (1 by default). The number 'val' must be an integer
between 0 and 2^nbbits-1.
Examples:
  assert(bit32.set(0xA, 0x12345678, 0, 4) == 0x1234567A)
  assert(bit32.set(0xFE, 0x12345678, 4, 8) == 0x12345FE8)
  assert(bit32.set(1, 0x12345678, 30) == 0x52345678)

Sure, these functions can be written in terms of bit32.band,
bit32.lshift, bit32.rshift and bit32.bor. But would they not be better
implemented in C inside the library?