Skip to content

core.base.bit

This module provides bitwise operations for Lua scripts in Xmake.

TIP

To use this module, you need to import it first: import("core.base.bit")

Overview

Although Lua 5.4+ natively supports bitwise operators, Xmake can be configured to use LuaJIT as the runtime. To ensure compatibility across different Lua versions, Xmake provides the bit module for more generic bitwise operations.

This module provides a common set of bitwise operations that work consistently regardless of whether you're using standard Lua or LuaJIT.

bit.band

  • Bitwise AND operation

Function Prototype

API

lua
bit.band(a: <number>, b: <number>)

Parameter Description

ParameterDescription
aRequired. First number
bRequired. Second number

Return Value

TypeDescription
numberReturns the bitwise AND result of a and b

Usage

lua
import("core.base.bit")

local result = bit.band(5, 3)  -- Returns 1

bit.bor

  • Bitwise OR operation

Function Prototype

API

lua
bit.bor(a: <number>, b: <number>)

Parameter Description

ParameterDescription
aRequired. First number
bRequired. Second number

Return Value

TypeDescription
numberReturns the bitwise OR result of a and b

Usage

lua
import("core.base.bit")

local result = bit.bor(5, 3)  -- Returns 7

bit.bxor

  • Bitwise XOR operation

Function Prototype

API

lua
bit.bxor(a: <number>, b: <number>)

Parameter Description

ParameterDescription
aRequired. First number
bRequired. Second number

Return Value

TypeDescription
numberReturns the bitwise XOR result of a and b

Usage

lua
import("core.base.bit")

local result = bit.bxor(5, 3)  -- Returns 6

bit.bnot

  • Bitwise NOT operation

Function Prototype

API

lua
bit.bnot(a: <number>)

Parameter Description

ParameterDescription
aRequired. Number to negate

Return Value

TypeDescription
numberReturns the bitwise NOT result of a

Usage

lua
import("core.base.bit")

local result = bit.bnot(5)  -- Returns the bitwise NOT of 5

bit.lshift

  • Left shift operation

Function Prototype

API

lua
bit.lshift(a: <number>, b: <number>)

Parameter Description

ParameterDescription
aRequired. Number to shift
bRequired. Number of bits to shift left

Return Value

TypeDescription
numberReturns a shifted left by b bits

Usage

lua
import("core.base.bit")

local result = bit.lshift(5, 2)  -- Returns 20 (5 << 2)

bit.rshift

  • Right shift operation

Function Prototype

API

lua
bit.rshift(a: <number>, b: <number>)

Parameter Description

ParameterDescription
aRequired. Number to shift
bRequired. Number of bits to shift right

Return Value

TypeDescription
numberReturns a shifted right by b bits

Usage

lua
import("core.base.bit")

local result = bit.rshift(20, 2)  -- Returns 5 (20 >> 2)

bit.tobit

  • Convert to 32-bit integer

Function Prototype

API

lua
bit.tobit(x: <number>)

Parameter Description

ParameterDescription
xRequired. Number to convert

Return Value

TypeDescription
numberReturns x masked to 32-bit integer (0xffffffff)

Usage

lua
import("core.base.bit")

local result = bit.tobit(0x12345678)  -- Returns value masked to 32 bits

bit.tohex

  • Convert number to hexadecimal string

Function Prototype

API

lua
bit.tohex(x: <number>, n?: <number>)

Parameter Description

ParameterDescription
xRequired. Number to convert
nOptional. Number of hexadecimal digits (default: 8). Use negative for uppercase

Return Value

TypeDescription
stringReturns hexadecimal string representation

Usage

lua
import("core.base.bit")

local hex = bit.tohex(255, 2)      -- Returns "ff"
local hex = bit.tohex(255, -2)     -- Returns "FF"
local hex = bit.tohex(255)         -- Returns "000000ff"