dreamworkAPI
Ctrl K
GitHub
libraryDocs / bit

bit

librarysharedmenusource

The bit library.

Functions

bit.arshiftReturns the number x arithmetically shifted disp bits to the right.
bit.bandReturns the bitwise AND of all provided values.
bit.bnotReturns the bitwise NOT ( negation ) of x.
bit.borReturns the bitwise OR of all provided values.
bit.bswapSwaps the byte order of a 32-bit integer.
bit.btestReturns whether the bitwise AND of all provided values is non-zero, i.e. whether x and every value in ... have at least…
bit.bxorReturns the bitwise XOR of all provided numbers.
bit.clzCounts the number of leading zero bits in x, starting from the most significant bit of the bit_count-bit window and sto…
bit.ctzCounts the number of trailing zero bits in x, starting from the least significant bit and stopping at the first 1 bit.
bit.extractExtracts a bit field of the given width from x, starting at bit position field (0-indexed, counted from the least signi…
bit.lshiftReturns the number x shifted disp bits to the left.
bit.majBitwise majority function.
bit.muxBitwise multiplexer (also known as "choose").
bit.replaceReplaces a bit field of the given width in x, starting at bit position field (0-indexed, counted from the least signifi…
bit.reverseReverses the order of the lowest bit_count bits of x, so the least significant bit becomes the most significant bit (wi…
bit.rolReturns the number x rotated disp bits to the left.
bit.rorReturns the number x rotated disp bits to the right.
bit.rshiftReturns the number x shifted disp bits to the right.
bit.signReturns the sign of an integer.
bit.tobitNormalizes the specified value to a signed 32-bit integer by wrapping it around modulo 2^32, so values outside the rang…
bit.tohexReturns the lowercase hexadecimal representation of the number, zero-padded to length digits.
bit.unsignReturns the unsigned value of an integer.
bit.widthReturns the number of bits needed to store x as an unsigned bit_count-bit integer, i.e. the position of its highest set…

#bit.arshift

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

Returns the number x arithmetically shifted disp bits to the right.

Unlike bit.rshift, the vacated high-order bits are filled with copies of the sign bit of x rather than zeros, so the sign of the value is preserved.

Negative displacements shift to the left, behaving identically to bit.lshift in that case.

lua
    1111 1000   -8  (as a signed 8-bit value)
ARSH 0000 0010   2
_____________
    1111 1110   -2
Parameters
NameTypeDescription
xTThe value to be manipulated.
dispintegerAmounts of bits to shift.
Returns
TypeNameDescription
TresultThe arithmetically shifted value.

#bit.band

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

Returns the bitwise AND of all provided values.

Each bit is tested against the following truth table:

ABOutput
000
100
010
111
lua
    1100 1110   222
    0101 1000   88
AND 0100 1001   73
_____________
    0100 1000   72
Parameters
NameTypeDescription
xTThe value to be manipulated.
...optionalTValues bit to perform bitwise AND with. Optional.
Returns
TypeNameDescription
TresultThe bitwise AND result between all values.

#bit.bnot

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

Returns the bitwise NOT ( negation ) of x.

Inverts every bit of the 32-bit integer.

lua
NOT 1111 0000   240
_____________
    0000 1111   15

<br/>

For any integer x, the following identity holds:

lua
assert( bit.bnot( x ) == ( -1 - x ) % 2 ^ 32 )
````
Parameters
NameTypeDescription
xTThe value to be manipulated/inverted.
Returns
TypeNameDescription
TresultThe result of bitwise NOT (0101 becomes 1010).

#bit.bor

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

Returns the bitwise OR of all provided values.

Each bit is tested against the following truth table:

ABOutput
000
101
011
111
lua
    0101 0001   81
    0001 0101   21
OR  0000 0101   5
_____________
    0101 0101   85
Parameters
NameTypeDescription
xTThe value to be manipulated.
...optionalTValues bit or with. Optional.
Returns
TypeNameDescription
TresultThe bitwise OR result between all values.

#bit.bswap

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

Swaps the byte order of a 32-bit integer.

Parameters
NameTypeDescription
xTThe 32-bit integer to be byte-swapped.
Returns
TypeNameDescription
TresultThe byte-swapped value.

#bit.btest

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

Returns whether the bitwise AND of all provided values is non-zero, i.e. whether x and every value in ... have at least one bit in common.

Equivalent to bit.band( x, ... ) ~= 0, but without needing to look at the result.

Parameters
NameTypeDescription
xTThe first value to test.
...integerAdditional values to test against the first. Optional.
Returns
TypeNameDescription
booleanresulttrue if any bit is set in every given value, false otherwise.

#bit.bxor

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

Returns the bitwise XOR of all provided numbers.

Each bit is tested against the following truth table:

ABOutput
000
101
011
110

<br/>

lua
    0101 0001   81
    0001 0101   21
XOR 0000 0101   5
_____________
    0100 0001   65
Parameters
NameTypeDescription
xTThe value to be manipulated.
...optionalTValues bit XOR with. Optional.
Returns
TypeNameDescription
TresultResult of bitwise XOR` operation.

#bit.clz

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

Counts the number of leading zero bits in x, starting from the most significant bit of the bit_count-bit window and stopping at the first 1 bit.

Returns bit_count if x is zero (i.e. all bits within the window are zero).

Parameters
NameTypeDescription
xTThe value to count the leading zeros of.
bit_countoptionalintegerThe width, in bits, of the window to inspect. Defaults to 32.
Returns
TypeNameDescription
TresultThe number of leading zero bits.

#bit.ctz

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

Counts the number of trailing zero bits in x, starting from the least significant bit and stopping at the first 1 bit.

Returns bit_count if x is zero (i.e. all bits within the window are zero).

Parameters
NameTypeDescription
xTThe value to count the trailing zeros of.
bit_countoptionalintegerThe width, in bits, of the window to inspect. Defaults to 32.
Returns
TypeNameDescription
TresultThe number of trailing zero bits.

#bit.extract

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

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

Equivalent to shifting x right by field bits and then masking off everything above width bits.

lua
extract( 0b10110100, 2, 3 ) --> 0b101 ( 5 )
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.

#bit.lshift

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

Returns the number x shifted disp bits to the left.

The number disp may be any representable integer.

Negative displacements shift to the right.

In any direction, vacant bits are filled with zeros.

In particular, displacements with absolute values higher than 31 result in zero (all bits are shifted out).

lua
    0000 1111   15
LSH 0000 0011   3
_____________
    0111 1000   120

NOTE: The returned value will be clamped to a signed 32-bit integer, even on 64-bit builds.

See this wiki article for more details.

Parameters
NameTypeDescription
xTThe value to be manipulated.
dispintegerAmounts of bits to shift left by.
Returns
TypeNameDescription
TresultThe left shifted value. Input of 0b1001 will become 0b10010 for one left shift, etc.

#bit.maj

sharedmenunodiscardsource
function bit.maj(a: T, b: T, c: T): T

Bitwise majority function. For each bit position, returns 1 if at least two of the three corresponding bits of a, b and c are 1, and 0 otherwise.

Equivalent to (a & (b | c)) | (b & c), and often used as the Maj function in hash algorithms such as SHA-2.

Parameters
NameTypeDescription
aTThe first value.
bTThe second value.
cTThe third value.
Returns
TypeNameDescription
TresultThe bitwise majority value.

#bit.mux

sharedmenunodiscardsource
function bit.mux(a: T, b: T, c: T): T

Bitwise multiplexer (also known as "choose"). For each bit position, selects the corresponding bit of b where a is 1, and the corresponding bit of c where a is 0.

Equivalent to c ~ (a & (b ~ c)), and often used as the Ch function in hash algorithms such as SHA-2.

Parameters
NameTypeDescription
aTThe selector value: a 1 bit picks from b, a 0 bit picks from c.
bTThe value to select bits from where a is 1.
cTThe value to select bits from where a is 0.
Returns
TypeNameDescription
TresultThe bitwise multiplexed value.

#bit.replace

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

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

All bits of x outside the targeted field are left unchanged.

Parameters
NameTypeDescription
xTThe value whose bit field is to be replaced.
extractintegerThe 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.

#bit.reverse

sharedmenunodiscardsource
function 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
reverse( 0b1011000, 7 ) --> 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.

#bit.rol

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

Returns the number x rotated disp bits to the left.

The number disp may be any representable integer.

For any valid displacement, the following identity holds:

lua
assert( bit.lrotate( x, disp ) == bit.lrotate( x, disp % 32 ) )
Parameters
NameTypeDescription
xTThe value to be manipulated.
dispintegerAmounts of bits to rotate left by.
Returns
TypeNameDescription
TresultThe left rotated value.

#bit.ror

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

Returns the number x rotated disp bits to the right.

The number disp may be any representable integer.

For any valid displacement, the following identity holds:

lua
assert( bit.rrotate( x, disp ) == bit.rrotate( x, disp % 32 ) )

In particular, negative displacements rotate to the left.

Parameters
NameTypeDescription
xTThe value to be manipulated.
dispintegerAmounts of bits to rotate right by.
Returns
TypeNameDescription
TresultThe right rotated value.

#bit.rshift

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

Returns the number x shifted disp bits to the right.

The number disp may be any representable integer.

Negative displacements shift to the left. In any direction, vacant bits are filled with zeros.

In particular, displacements with absolute values higher than 31 result in zero (all bits are shifted out).

lua
    0111 1000   120
RSH 0000 0011   3
_____________
    0000 1111   15

NOTE: The returned value will be clamped to a signed 32-bit integer, even on 64-bit builds.

See this wiki article for more details.

Parameters
NameTypeDescription
xTThe value to be manipulated.
dispintegerAmounts of bits to shift right by.
Returns
TypeNameDescription
TresultThe right shifted value.

#bit.sign

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

Returns the sign of an integer.

Parameters
NameTypeDescription
xTThe integer to get the sign of.
bit_countoptionalintegerThe amount of bits to unsign to, 32 by default.
Returns
TypeNameDescription
TresultThe sign of the integer: 1 for positive, 0 for zero, -1 for negative.

#bit.tobit

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

Alias of raw.bit.tobit.

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.

#bit.tohex

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

Alias of raw.bit.tohex.

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.

#bit.unsign

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

Returns the unsigned value of an integer.

Parameters
NameTypeDescription
xTThe integer to get the unsigned value of.
bit_countoptionalintegerThe amount of bits to unsign to, 32 by default.
Returns
TypeNameDescription
TresultThe unsigned value of the integer.

#bit.width

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

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

Equivalent to bit_count - bit.clz( x, bit_count ), returns 0 if x is zero.

lua
bit.width( 0 )    --> 0
bit.width( 255 )  --> 8
bit.width( 256 )  --> 9
Parameters
NameTypeDescription
xTThe value to measure.
bit_countoptionalintegerThe width, in bits, of the window to inspect. Defaults to 32.
Returns
TypeNameDescription
integerwidthThe number of significant bits, from 0 to bit_count.
Generated from the dreamwork meta branch.