Skip to content

Bitwise Operations

StationeersLua provides 53-bit-safe bitwise operations matching IC10's numeric precision.

Basic Operations

lua
local result = bit_and(a, b)    -- Bitwise AND
local result = bit_or(a, b)     -- Bitwise OR
local result = bit_xor(a, b)    -- Bitwise XOR
local result = bit_nor(a, b)    -- Bitwise NOR
local result = bit_not(a)       -- Bitwise NOT

Bit Shifts

lua
local result = bit_sll(a, n)    -- Shift left logical
local result = bit_srl(a, n)    -- Shift right logical (unsigned)
local result = bit_sra(a, n)    -- Shift right arithmetic (signed)

Bit Fields

lua
-- Extract a bit field: n bits starting at position pos
local field = bit_ext(value, pos, len)

-- Insert src into dst at position pos, width len
local result = bit_ins(dst, src, pos, len)

Example: Packing Flags

lua
local flags = 0
flags = bit_or(flags, bit_sll(1, 0))  -- Set bit 0 (power on)
flags = bit_or(flags, bit_sll(1, 3))  -- Set bit 3 (alert active)

-- Check if bit 3 is set
if bit_and(flags, bit_sll(1, 3)) ~= 0 then
    print("Alert is active")
end

Function Reference

FunctionReturnsDescription
bit_and(a, b)numberBitwise AND
bit_or(a, b)numberBitwise OR
bit_xor(a, b)numberBitwise XOR
bit_nor(a, b)numberBitwise NOR
bit_not(a)numberBitwise NOT
bit_sll(a, n)numberShift left logical
bit_srl(a, n)numberShift right logical
bit_sra(a, n)numberShift right arithmetic
bit_ext(val, pos, len)numberExtract bit field
bit_ins(dst, src, pos, len)numberInsert bit field

⚠️ This documentation was AI-generated and may contain inaccuracies. Please submit pull requests with corrections as needed.