lua-users home
lua-l archive

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


On Jan 11, 2010, at 10:22 AM, Roberto Ierusalimschy wrote:

> About the system-dependent semantics, I may be missing something,
> but I do not see the problem. This library operates on bits; as long
> as the result type is able to represent the 32 bits, is should make
> no difference whether they are signed, unsigned, or packed inside a
> double. Of course, if you do something like "if bit.band(x,y) < 0
> ...", then you will have system-dependent semantics. But, for me, this
> operation already has an arbitrary semantics. What does it mean to ask
> whether a bunch of bits is smaller than zero?

In my "Plea" at http://lua-users.org/wiki/BitwiseOperators

one of my laments was the inability to portably convert an arbitrary sized bit field to a signed number. This is often needed in embedded applications when interfacing with hardware or a pre-defined C struct. It may also be useful when extracting information from communication packets. 

One way to provide this is a sign-extend function, say bit.signextend (x,i). On the wiki page I show a simple (branch free) way to do this in C assuming the bit library returns an unsigned number:

sext = 1 << (i - 1); 
return (x ^ sext) - sext;

This can also be done in Lua as long as the bit library has well defined and portable semantics.

It's unfortunate that LuaJit 2 and Lua 5.2 (work1) have different bit semantics, and it would be great for Lua users if the authors could resolve those differences.

e