dreamworkAPI
Ctrl K
GitHub
libraryDocs / raw / bit

raw.bit

librarysharedmenusource

Native 32-bit integer only bit library.

Fields

NameTypeDescription
operatorsbooleanWhether the bit operators are available.

Functions

raw.bit.arshiftPerforms an arithmetic right shift of the specified 32-bit integer by the given number of bits.
raw.bit.bandPerforms a bitwise AND operation on all the specified 32-bit integers.
raw.bit.bnotPerforms a bitwise NOT (one's complement) operation on the specified 32-bit integer.
raw.bit.borPerforms a bitwise OR operation on all the specified 32-bit integers.
raw.bit.brotateRotates the bits of the specified 32-bit integer.
raw.bit.bshiftPerforms a logical bit shift of the specified 32-bit integer.
raw.bit.bswapSwaps the byte order (endianness) of the specified 32-bit integer, reversing the order of its four constituent bytes.
raw.bit.btestPerforms a bitwise AND operation on all the specified 32-bit integers and returns whether the result is non-zero.
raw.bit.bxorPerforms a bitwise XOR (exclusive OR) operation on all the specified 32-bit integers.
raw.bit.extractExtracts a bit field of the given width from the specified 32-bit integer, starting at the given bit position (0-indexe…
raw.bit.lrotateRotates the bits of the specified 32-bit integer to the left by the given number of bits.
raw.bit.lshiftPerforms a logical left shift of the specified 32-bit integer by the given number of bits.
raw.bit.replaceReplaces a bit field of the given width in the specified 32-bit integer, starting at the given bit position (0-indexed,…
raw.bit.reverseReverses the order of the lowest bit_count bits of x, so the least significant bit becomes the most significant bit (wi…
raw.bit.rrotateRotates the bits of the specified 32-bit integer to the right by the given number of bits.
raw.bit.rshiftPerforms a logical right shift of the specified 32-bit integer by the given number of bits.
raw.bit.signConverts an unsigned bit_count-bit integer to its signed (two's complement) value, the inverse of rbit.unsign.
raw.bit.tobitNormalizes the specified value to a signed 32-bit integer by wrapping it around modulo 2^32, so values outside the rang…
raw.bit.tohexReturns the lowercase hexadecimal representation of the number, zero-padded to length digits.
raw.bit.unsignConverts a signed bit_count-bit integer to its unsigned value, the inverse of rbit.sign.
raw.bit.widthReturns the number of bits needed to store the specified 32-bit integer as unsigned, i.e. the position of its highest s…

#raw.bit.arshift

sharedmenunodiscardsource
function raw.bit.arshift(x: T, disp: integer): T

Performs an arithmetic right shift of the specified 32-bit integer by the given number of bits. Bits shifted out of the bottom are discarded and the sign bit is copied into the vacated high-order bits, preserving the sign of the value.

Parameters
NameTypeDescription
xTThe value to be shifted.
dispintegerThe number of bits to shift right by.
Returns
TypeNameDescription
TresultThe shifted value, normalized to a signed 32-bit integer.

#raw.bit.band

sharedmenunodiscardsource
function raw.bit.band(x: T, ...: T): T

Performs a bitwise AND operation on all the specified 32-bit integers.

Parameters
NameTypeDescription
xTThe first value.
...TAdditional values to AND together with the first.
Returns
TypeNameDescription
TresultThe bitwise AND of all given values, normalized to a signed 32-bit integer.

#raw.bit.bnot

sharedmenunodiscardsource
function raw.bit.bnot(x: T): T

Performs a bitwise NOT (one's complement) operation on the specified 32-bit integer.

Parameters
NameTypeDescription
xTThe value to be inverted.
Returns
TypeNameDescription
TresultThe bitwise complement of the given value, normalized to a signed 32-bit integer.

#raw.bit.bor

sharedmenunodiscardsource
function raw.bit.bor(x: T, ...: T): T

Performs a bitwise OR operation on all the specified 32-bit integers.

Parameters
NameTypeDescription
xTThe first value.
...TAdditional values to OR together with the first.
Returns
TypeNameDescription
TresultThe bitwise OR of all given values, normalized to a signed 32-bit integer.

#raw.bit.brotate

sharedmenunodiscardsource
function raw.bit.brotate(x: T, disp: integer): T

Rotates the bits of the specified 32-bit integer. A positive displacement rotates left, a negative displacement rotates right.

Parameters
NameTypeDescription
xTThe value to be rotated.
dispintegerThe number of bits to rotate by. Positive rotates left, negative rotates right.
Returns
TypeNameDescription
TresultThe rotated value, normalized to a signed 32-bit integer.

#raw.bit.bshift

sharedmenunodiscardsource
function raw.bit.bshift(x: T, disp: integer): T

Performs a logical bit shift of the specified 32-bit integer. A positive displacement shifts left, a negative displacement shifts right.

Parameters
NameTypeDescription
xTThe value to be shifted.
dispintegerThe number of bits to shift by. Positive shifts left, negative shifts right.
Returns
TypeNameDescription
TresultThe shifted value, normalized to a signed 32-bit integer.

#raw.bit.bswap

sharedmenunodiscardsource
function raw.bit.bswap(x: T): T

Swaps the byte order (endianness) of the specified 32-bit integer, reversing the order of its four constituent bytes.

Parameters
NameTypeDescription
xTThe value whose byte order is to be swapped.
Returns
TypeNameDescription
TresultThe byte-swapped value, normalized to a signed 32-bit integer.

#raw.bit.btest

sharedmenunodiscardsource
function raw.bit.btest(x: T, ...: T): boolean

Performs a bitwise AND operation on all the specified 32-bit integers and returns whether the result is non-zero.

Parameters
NameTypeDescription
xTThe first value.
...TAdditional values to AND together with the first.
Returns
TypeNameDescription
booleanresulttrue if the bitwise AND of all given values is non-zero, false otherwise.

#raw.bit.bxor

sharedmenunodiscardsource
function raw.bit.bxor(x: T, ...: T): T

Performs a bitwise XOR (exclusive OR) operation on all the specified 32-bit integers.

Parameters
NameTypeDescription
xTThe first value.
...TAdditional values to XOR together with the first.
Returns
TypeNameDescription
TresultThe bitwise XOR of all given values, normalized to a signed 32-bit integer.

#raw.bit.extract

sharedmenunodiscardsource
function raw.bit.extract(x: T, field: integer, width?: integer): T

Extracts a bit field of the given width from the specified 32-bit integer, starting at the given bit position (0-indexed, counted from the least significant bit).

lua
rbit.extract( 0xB4, 2, 3 ) -- 5 (0b10110100 -> 0b101)
Parameters
NameTypeDescription
xTThe value to extract the bit field from.
fieldintegerThe starting bit position of the field (0-indexed).
widthoptionalintegerThe number of bits in the field. Defaults to 1.
Returns
TypeNameDescription
TresultThe extracted bit field, right-aligned as an unsigned integer.

#raw.bit.lrotate

sharedmenunodiscardsource
function raw.bit.lrotate(x: T, disp: integer): T

Rotates the bits of the specified 32-bit integer to the left by the given number of bits. Bits shifted out of the top are wrapped around and shifted in at the bottom.

Parameters
NameTypeDescription
xTThe value to be rotated.
dispintegerThe number of bits to rotate left by.
Returns
TypeNameDescription
TresultThe rotated value, normalized to a signed 32-bit integer.

#raw.bit.lshift

sharedmenunodiscardsource
function raw.bit.lshift(x: T, disp: integer): T

Performs a logical left shift of the specified 32-bit integer by the given number of bits. Bits shifted out of the top are discarded and zero bits are shifted in from the bottom.

Parameters
NameTypeDescription
xTThe value to be shifted.
dispintegerThe number of bits to shift left by.
Returns
TypeNameDescription
TresultThe shifted value, normalized to a signed 32-bit integer.

#raw.bit.replace

sharedmenunodiscardsource
function raw.bit.replace(x: T, extract: T, field: integer, width?: integer): T

Replaces a bit field of the given width in the specified 32-bit integer, starting at the given bit position (0-indexed, counted from the least significant bit), with the bits taken from extract.

Parameters
NameTypeDescription
xTThe value whose bit field is to be replaced.
extractTThe value providing the replacement bits (only the lowest width bits are used).
fieldintegerThe starting bit position of the field to replace (0-indexed).
widthoptionalintegerThe number of bits in the field. Defaults to 1.
Returns
TypeNameDescription
TresultThe value of x with the specified bit field replaced.

#raw.bit.reverse

sharedmenunodiscardsource
function raw.bit.reverse(x: T, bit_count?: integer): T

Reverses the order of the lowest bit_count bits of x, so the least significant bit becomes the most significant bit (within that range) and vice versa.

lua
rbit.reverse( 0x58, 7 ) -- 13 (0b1011000 -> 0b0001101)
Parameters
NameTypeDescription
xTThe value whose bits are to be reversed.
bit_countoptionalintegerThe number of low-order bits to reverse. Defaults to 32.
Returns
TypeNameDescription
TresultThe value of x with its bits reversed.

#raw.bit.rrotate

sharedmenunodiscardsource
function raw.bit.rrotate(x: T, disp: integer): T

Rotates the bits of the specified 32-bit integer to the right by the given number of bits. Bits shifted out of the bottom are wrapped around and shifted in at the top.

Parameters
NameTypeDescription
xTThe value to be rotated.
dispintegerThe number of bits to rotate right by.
Returns
TypeNameDescription
TresultThe rotated value, normalized to a signed 32-bit integer.

#raw.bit.rshift

sharedmenunodiscardsource
function raw.bit.rshift(x: T, disp: integer): T

Performs a logical right shift of the specified 32-bit integer by the given number of bits. Bits shifted out of the bottom are discarded and zero bits are shifted in from the top, regardless of the sign of the value.

Parameters
NameTypeDescription
xTThe value to be shifted.
dispintegerThe number of bits to shift right by.
Returns
TypeNameDescription
TresultThe shifted value, normalized to a signed 32-bit integer.

#raw.bit.sign

sharedmenunodiscardsource
function raw.bit.sign(x: T, bit_count?: integer): T

Converts an unsigned bit_count-bit integer to its signed (two's complement) value, the inverse of rbit.unsign.

Every positive x has 2 ^ bit_count subtracted from it, so only pass values that have the sign bit set; zero and negative values are returned unchanged.

lua
rbit.sign( 0xFFFFFFFF ) -- -1
rbit.sign( 0xFF, 8 ) -- -1
Parameters
NameTypeDescription
xTThe unsigned integer to convert.
bit_countoptionalintegerThe bit width of the integer, 32 by default.
Returns
TypeNameDescription
TresultThe signed value of the integer.

#raw.bit.tobit

sharedmenunodiscardsource
function raw.bit.tobit(x: (integer | ffi.cdata*)): integer

Normalizes the specified value to a signed 32-bit integer by wrapping it around modulo 2^32, so values outside the range are not clamped.

lua
rbit.tobit( 0xFFFFFFFF ) -- -1
rbit.tobit( 2 ^ 32 + 5 ) -- 5
Parameters
NameTypeDescription
x(integer | ffi.cdata*)The value to be normalized.
Returns
TypeNameDescription
integerresultThe normalized value, in the range -2147483648 to 2147483647.

#raw.bit.tohex

sharedmenunodiscardsource
function raw.bit.tohex(x: (integer | ffi.cdata*), length?: integer): string

Returns the lowercase hexadecimal representation of the number, zero-padded to length digits.

lua
rbit.tohex( 255 ) -- "000000ff"
rbit.tohex( 255, 2 ) -- "ff"
Parameters
NameTypeDescription
x(integer | ffi.cdata*)The value to be converted.
lengthoptionalintegerThe number of digits, must be positive. Defaults to 8.
Returns
TypeNameDescription
stringstrThe hexadecimal representation.

#raw.bit.unsign

sharedmenunodiscardsource
function raw.bit.unsign(x: T, bit_count?: integer): T

Converts a signed bit_count-bit integer to its unsigned value, the inverse of rbit.sign.

Negative values have 2 ^ bit_count added to them; zero and positive values are returned unchanged.

lua
rbit.unsign( -1 ) -- 4294967295
rbit.unsign( -1, 8 ) -- 255
Parameters
NameTypeDescription
xTThe signed integer to convert.
bit_countoptionalintegerThe bit width of the integer, 32 by default.
Returns
TypeNameDescription
TresultThe unsigned value of the integer.

#raw.bit.width

sharedmenunodiscardsource
function raw.bit.width(x: integer): integer

Returns the number of bits needed to store the specified 32-bit integer as unsigned, i.e. the position of its highest set bit plus one.

Returns 0 for 0 and 32 for negative values (they are read as unsigned).

lua
rbit.width( 0 ) -- 0
rbit.width( 255 ) -- 8
rbit.width( 256 ) -- 9
Parameters
NameTypeDescription
xintegerThe value to measure.
Returns
TypeNameDescription
integerwidthThe number of significant bits, from 0 to 32.
Generated from the dreamwork meta branch.