Skip to content

Bit

These functions provide bitwise operations and manipulations for 32-bit integers.

Note: This bit library returns signed integers. If you need unsigned output, use the built-in bit32 library instead. Use bit for signed output and bit32 for unsigned output.

bit.badd(...numbers: number): number

Adds multiple numbers bitwise.

local result = bit.badd(1, 2, 3)

Returns: The bitwise sum of all input numbers.

bit.bsub(...numbers: number): number

Subtracts multiple numbers bitwise, starting from the first number.

local result = bit.bsub(10, 3, 2)

Returns: The bitwise subtraction result.

bit.bmul(...numbers: number): number

Multiplies multiple numbers bitwise.

local result = bit.bmul(2, 3, 4)

Returns: The bitwise multiplication result.

bit.bdiv(...numbers: number): number

Divides multiple numbers bitwise, starting from the first number.

local result = bit.bdiv(100, 2, 5)

Returns: The bitwise division result.

bit.band(...numbers: number): number

Performs a bitwise AND operation on all input numbers.

local result = bit.band(0xFF, 0xF0)

Returns: The result of the bitwise AND operation.

bit.bor(...numbers: number): number

Performs a bitwise OR operation on all input numbers.

local result = bit.bor(0x0F, 0xF0)

Returns: The result of the bitwise OR operation.

bit.bxor(...numbers: number): number

Performs a bitwise XOR operation on all input numbers.

local result = bit.bxor(0xFF, 0x0F)

Returns: The result of the bitwise XOR operation.

bit.bnot(number: number): number

Performs a bitwise NOT operation on the input number.

local result = bit.bnot(0xF)

Returns: The result of the bitwise NOT operation.

bit.lshift(value: number, shift: number): number

Performs a left shift operation.

local result = bit.lshift(1, 4)

Returns: The result of the left shift operation.

bit.rshift(value: number, shift: number): number

Performs a logical right shift operation.

local result = bit.rshift(16, 2)

Returns: The result of the logical right shift operation.

bit.arshift(value: number, shift: number): number

Performs an arithmetic right shift operation.

local result = bit.arshift(-8, 1)

Returns: The result of the arithmetic right shift operation.

bit.rol(value: number, shift: number): number

Performs a left rotate operation.

local result = bit.rol(0x12345678, 4)

Returns: The result of the left rotate operation.

bit.ror(value: number, shift: number): number

Performs a right rotate operation.

local result = bit.ror(0x12345678, 4)

Returns: The result of the right rotate operation.

bit.bpopcount(value: number): number

Counts the number of set bits in the input value.

local count = bit.bpopcount(0xFF)

Returns: The number of set bits.

bit.bswap(value: number): number

Swaps the bytes in the input value.

local swapped = bit.bswap(0x12345678)

Returns: The value with swapped bytes.

bit.tohex(value: number, [nibbles: number]): string

Converts a number to its hexadecimal representation.

local hex = bit.tohex(0xABCD, 4)

Returns: A string representing the hexadecimal value.

bit.tobit(value: number): number

Converts a number to a 32-bit integer.

local int32 = bit.tobit(0xFFFFFFFF)

Returns: The 32-bit integer representation of the input value.