lua-users home
lua-l archive

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


On Wednesday 31 March 2004 07:32, Alex Sandro Queiroz e Silva wrote:

> Goetz.Kluge@infineon.com wrote:
> >  if (l1 < l2) l1 = l2;

>     Shouldn't it be the other way around? This way you are using the
> size of the largest string, not the smallest's.

I believe you're correct; the conditional should read (l1 > l2).

I wrote something similar, though I put it in a separate bstring module that 
also keeps track of how many bits are significant. In my code, I chose to use 
the larger of the two and to assume that the shorter was padded with 0's. 
It's hard to say which is better, or whether throwing an error is better. I 
overload concatenation, too. More on this below.

Manipulating strings of bits is very, very useful in communications circles. 
It makes for easy interactive testing and building of test scripts.

It also helps to have Lua recognize hexadecimal integers in C syntax, and to 
have it recognize hex escape sequences in strings. I've added both of those 
features to my version of Lua. I wish it were built-in, especially since it's 
so small. But I've got a patch in my Lua build script that has worked on 5.0, 
5.0.1, and 5.0.2 without change. I haven't tried 5.1 yet. I'm still trying to 
get my build script to statically link the standard libraries, since I plan 
to port it all to a platform that does not support dynamic loading.

I can use Reuben's bit-wise functions from the standard library:

>>> bitlib,e=loadlib("/usr/local/lib/libluaRRT.so","luaopen_bit")
>>> bitlib()
> require"std"
> print(string.format("0x%06X",bit.band(0x123456,0xFFEE22)))
0x122402
> print(string.format("0x%06X",bit.bxor(0x123456,0xFFEE22)))
0xEDDA74
> 

In my implementation of strings of bits, each bstring is an object of its own. 
The bits are stored lsb-first (which is usual in comm).

Here's the interface, using the names found in std.bit whenever possible:

  -- These return new bstring objects:
  function bstring:new(source_string,bits)
  function bstring.band(self,bs2)
  function bstring.bor(self,bs2)
  function bstring.bxor(self,bs2)
  function bstring.bnot(self)
  function bstring.sub(self,i[,j])  -- 0-based index (usual for my field)
  function bstring.concat(bs1,bs2)  -- overload for '..'

  -- Then there are these:
  function bstring.len(self)     -- number of bits
  function bstring.bytes(self)   -- string data bytes
  function bstring.hex(self)     -- string data with hex escape sequences
  function bstring.dumphex(self) -- pretty-printed string data in hex
  function bstring.bin(self)     -- ASCII 0's and 1's of bstring.len(self)
  function bstring.dumpbin(self) -- pretty-printed ASCII 0's and 1's

Eventually I will add code to handle regular strings as arguments, assuming a 
bit count of 8 * length. Plus I'd like to add bstring.find(), .gsub(), etc., 
with bit patterns and hamming distances for search criteria. I already have a 
correlator, but it returns an array of bit-error counts which may then be 
sorted. It would be nice to build this into the patterns of .find() somehow. 
Any suggestions are welcome.

Initially I wrote this on my Palm Pilot while waiting in various lines, like 
when I renewed my driver's license. I used simple ASCII strings of '0' and 
'1', but kept the interface the same. It was very slow, but still useful in 
doing real work when away from the office.

I'm in the middle of making some modifications and writing test scripts for 
the package, then I'll release the code. It will require a C library with it.

The Palm Pilot version is for Plua (4.0) and is all in Lua; it implements only 
a subset of the above. It's not the prettiest code - heck, it was written in 
line at the DMV! - but if you're interested I'll send it to you.

Doug Rogers