Dialectronics - the dialect of electronic communications

About Us

Our Mission





XCOFF Bootloader

Old World Macs

Open Firmware



Home Repair

Top Level

Lua Corner

We think Lua is a _great_ programming language. It is extensible and BSD-licensed, which is consistent with our beliefs of leadership by example. While developing our BSD-licensed replacements for binutils, we needed several bitwise operators that are not built into Lua (Lua is not strong on math, but more than makes up for it with superior string manipulation). While there are several modules that can be incorporated into Lua, each had different syntax and required compiling. We decided to write bitwise operators in Lua.

BinDecHex.lua is the result of this effort. The conversion functions have been tested extensively against decoding PowerPC 4 byte instructions for the upcoming "ldis," a Lua-based BSD-licensed replacement for objdump and otool. BinDecHex contains the following Big-endian functions:

These convert binary<->decimal<->hexadecimal

Dec2Bin(s [, num])

With the exception of Dec2Bin, each function takes the value (as a string) and converts it, returning a string. Dec2Bin optionally takes a number indicating how many bits to extend the string.

These do And, NAnd, OR, XOR, and Not (NOTE: BMAnd and BMOr are well tested, the others less tested)

BMAnd(v, m)
BMNAnd(v, m)
BMOr(v, m)
BMXOr(v, m)
BMNot(v, m)

The first parameter is the value (a hex string), and the second is the mask (also a hex string). BMNot toggles bits (off to on, on to off) as specified by the mask and leaves the rest alone.

These functions shift right and left, adding zeros to lost or gained bits returned values are 32 bits long (fully tested):

BShRight(v, nb)
BShLeft(v, nb)

nb specifies the number of bits to shift in either direction.

In order to use the functions, you have to "require" them. Enter Lua and then type
> require "BinDecHex"

Then, each function is accessed through the table BinDecHex.

> print(BinDecHex.Hex2Dec("100"))