Bitwise.lua

From Moondust Wiki
Revision as of 09:57, 26 October 2015 by Rockythechao (talk | contribs)
Jump to navigation Jump to search

Download Latest Version

Bitwise is a library that provides access to bitwise operators.

Installation

Place the file bitwise.lua into the LuaScriptsLib folder.


How to use

To enable the Bitwise library for a specific level, add this line to lunadll.lua:

local bitwise = loadAPI("bitwise");

This will load the Bitwise API.

Example

local bitwise = loadAPI("bitwise");
    
function onLoad()
    local x = bitwise.bitand(16, 17);
    --Calculates the bitwise and of 10000 (16) and 10001 (17), which is 10000 (16).
end

Documentation

Structures

BitTable

BitTables aren't really their own structure, just specific formatting of tables. Formats other than this may cause errors or crashes in the code, and are not useful.

BitTables are tables of tables, which act to define the conditions under which variables should be true, or false.

Unary BitTable This is a BitTable for use with a unary operator, such as "not". This should be formatted like so:

local unary = 
{
    0,
    1
}

This will define the null BitTable, which returns 1 when the bit is 1, and 0 when the bit is 0.

Binary BitTable This is a BitTable for use with a binary operator, such as "and". These are arguably more useful than unary operators, and act on two bits at once. This should be formatted like so:

local binary = 
{
    {0,0},
    {1,1}
}

This will define one null BitTable, which returns 1 when the bit from the first value is 1, and 0 when the bit from the first value is 0.

BitTables are much like mathematical tables, and act as truth tables for the bitwise operators.

External Use Functions

These are functions you will need to use the library

invert table
Inverts a BitTable to produce the logical opposite. BitTable BitTable

The BitTable to invert.

bitunary value table
Runs a unary bitwise operation on the value, defined by the given table. number number

The value to operate on.

BitTable

The BitTable that defines the logical truth table for this operation.

bitbinary value a value b table
Runs a binary bitwise operation on the values, defined by the given table. number number

The first value to operate on.

number

The second value to operate on.

BitTable

The BitTable that defines the logical truth table for this operation.

leftshift value shift
Shifts the bits in the binary representation of the value to the left by a given amount. number number

The value to shift.

number

The number of places to shift.

rightshift value shift
Shifts the bits in the binary representation of the value to the right by a given amount. number number

The value to shift.

number

The number of places to shift.

cleftshift value shift
Shifts the bits in the binary representation of the value to the left by a given amount, wrapping around if any spill off the end (this function is experimental). number number

The value to shift.

number

The number of places to shift.

crightshift value shift
Shifts the bits in the binary representation of the value to the right by a given amount, wrapping around if any spill off the end (this function is experimental). number number

The value to shift.

number

The number of places to shift.

bitnot value
Inverts all the bits in the given value. number number

The value to operate on.

bitand value a value b
Performs a logical AND on all bits in both values. number number

The first value to operate on.

number

The second value to operate on.

bitnand value a value b
Performs a logical NOT AND on all bits in both values. number number

The first value to operate on.

number

The second value to operate on.

bitor value a value b
Performs a logical OR on all bits in both values. number number

The first value to operate on.

number

The second value to operate on.

bitnor value a value b
Performs a logical NOT OR on all bits in both values. number number

The first value to operate on.

number

The second value to operate on.

bitxor value a value b
Performs a logical XOR on all bits in both values. number number

The first value to operate on.

number

The second value to operate on.