Bitwise Operators

lua-users home
wiki

The standard version of Lua does not include bitwise operators. However, there are Lua libraries for this as well as some patched versions of Lua.

Implementations of bitwise operators in Lua:

bitlib - Sample Operations (release 21)

bit.bnot(a)       -- returns the one's complement of a
bit.band(w1,...)  -- returns the bitwise and of the w's
bit.bor(w1,...)   -- returns the bitwise or of the w's
bit.bxor(w1,...)  -- returns the bitwise exclusive or of the w's
bit.lshift(a,b)   -- returns a shifted left b places
bit.rshift(a,b)   -- returns a shifted logically right b places
bit.arshift(a,b)  -- returns a shifted arithmetically right b places
bit.mod(a,b)      -- returns the integer remainder of a divided by b

LuaBit - Sample Operations (release 0.4)

bit.bnot(n) -- bitwise not (~n)
bit.band(m, n) -- bitwise and (m & n)
bit.bor(m, n) -- bitwise or (m | n)
bit.bxor(m, n) -- bitwise xor (m ^ n)
bit.brshift(n, bits) -- right shift (n >> bits)
bit.blshift(n, bits) -- left shift (n << bits)
bit.blogic_rshift(n, bits) -- logic right shift(zero fill >>>)

BinDecHex - Sample Operations (2007-10)

Hex2Bin(s)
Bin2Hex(s)
Hex2Dec(s)
Dec2Hex(s)
Bin2Dec(s)
Dec2Bin(s [, num])
BMAnd(v, m)
BMNAnd(v, m)
BMOr(v, m)
BMXOr(v, m)
BMNot(v, m) 

Bitwise Operators Power Patch - Sample Operations

Infix bitwise operators for AND (&, __and), OR (|, __or), XOR (^^, __xor).
Infix bitwise operators for SHIFT LEFT (<<, __shl) and SHIFT RIGHT (>>, __shr).
Unary bitwise negation operator (~, __not).
Infix arithmetic operator for INTEGER DIVISION (\, __intdiv).
    accepts both ~= and != for comparison. 

RiscLua - Sample Operations

RiscLua uses hex literals and has bitwise operators, each with a corresponding event:

                        x&y                __bit_and
                        x|y                __bit_or
                        x^^y               __bit_xor
                        ~x                 __bit_not
It also has arithmetic shifts:

                        x<<y              __bit_lshift
                        x>>y              __bit_rshift

hextype.patch Sample Operations

| & ^^ << >> ~ \\ !=

Plea

The implementations above deal differently (or not at all in some cases) with two issues:

One of the implementations assumes a 32 bit integer, and provides signed and unsigned right shifts.

Another uses long long and unsigned long long and depends on the C compiler to perform right shifts correctly in each case (it happens to work great for me on x86 with gcc, but C89 and C99 don't specify how signed right shifts are performed; it is "implementation-defined behavior").

A third seems to completely ignore the issue and gives us whatever the C compiler decides to do with lua_Integers.

Note that when adding operators, Java defines the >> operator to use sign extension and the >>> operator to use zero extension.

None of them seem to provide a way to portably convert an arbitrary sized bit field to a signed number.

[This] patch would be great if bitfield took another optional boolean argument to indicate that the result should be sign extended to the full size of a lua_Integer.

BTW, a branch free way to sign extend a bitfield (Henry S. Warren Jr., CACM v20 n6 June 1977) is this:

sext = 1 << (len - 1); i = (i ^ sext) - sext;
assuming i is right aligned and all bits to the left of the field are zero; this is the case in the patch above where this would be nice (where sextp is the optional argument indicating that sign extension is desired):
if (len < LI_BITS)
{ i &= LI_MASK(len);
  if (sextp) 
  { lua_Integer sext = 1 << (len - 1);
    i = (i ^ sext) - sext;
  }
}

Note

There are at least two very different reasons for bitwise operations: set operations on a small universe of members, or manipulation of fields in explicit representations of data defined externally to the Lua program. The referenced implementations of bitwise operators are designed primarily for the later. For an implementation of the former, see Asko's Enum/bit operations patch in LuaPowerPatches

Comments

For a language intended to be interfaced to C/C++, it's strange that bitwise operators are not part of the syntax. Sure, they can be implemented as functions -- but this is clumsy.

Note: hex literals were added in Lua 5.1: 0xfe.

"How can bitwise operations be performed?" is a LuaFaq question.

Java has an abstraction of bit vectors called EnumSet [1]. Something similar might be done on Lua.


FindPage · RecentChanges · preferences
edit · history
Last edited November 1, 2007 12:33 am GMT (diff)