This module provides bitwise operations in Lua. It defines a table bit
with various bitwise functions. This module is based on the bitewise operations in Lua 5.2. Made usable for lua 5.1.
To use this module, follow these steps:
-
In your Lua script, import the
bit
module:local bit = require("cbit")
-
Create an instance of
bit
usingbit.new()
:local b = bit.new()
-
Perform bitwise operations using the available functions. For example, to perform a bitwise AND operation between two numbers:
local result = b:band(5, 3) -- Performs bitwise AND operation: 5 AND 3 print(result) -- Output: 1
The bit
module provides the following functions:
band(a, b)
: Performs a bitwise AND operation betweena
andb
.bor(a, b)
: Performs a bitwise OR operation betweena
andb
.bxor(a, b)
: Performs a bitwise XOR operation betweena
andb
.bnot(a)
: Performs a bitwise NOT operation ona
.lshift(a, n)
: Performs a left shift operation ona
byn
bits.rshift(a, n)
: Performs a right shift operation ona
byn
bits.arshift(a, b)
: Performs a right shift operation ona
byb
bits, preserving the sign bit.lrotate(a, n)
: Performs a left rotation operation ona
byn
bits.rrotate(a, n)
: Performs a right rotation operation ona
byn
bits.replace(a, b, start, stop)
: Replaces the bits ina
fromstart
tostop
with the bits inb
.extract(a, start, stop)
: Extracts the bits ina
fromstart
tostop
.btest(a, b)
: Tests if the bit at positionb
ina
is set.