bit
The bit library.
Functions
bit.arshift | Returns the number x arithmetically shifted disp bits to the right. |
bit.band | Returns the bitwise AND of all provided values. |
bit.bnot | Returns the bitwise NOT ( negation ) of x. |
bit.bor | Returns the bitwise OR of all provided values. |
bit.bswap | Swaps the byte order of a 32-bit integer. |
bit.btest | Returns whether the bitwise AND of all provided values is non-zero, i.e. whether x and every value in ... have at least… |
bit.bxor | Returns the bitwise XOR of all provided numbers. |
bit.clz | Counts the number of leading zero bits in x, starting from the most significant bit of the bit_count-bit window and sto… |
bit.ctz | Counts the number of trailing zero bits in x, starting from the least significant bit and stopping at the first 1 bit. |
bit.extract | Extracts a bit field of the given width from x, starting at bit position field (0-indexed, counted from the least signi… |
bit.lshift | Returns the number x shifted disp bits to the left. |
bit.maj | Bitwise majority function. |
bit.mux | Bitwise multiplexer (also known as "choose"). |
bit.replace | Replaces a bit field of the given width in x, starting at bit position field (0-indexed, counted from the least signifi… |
bit.reverse | Reverses the order of the lowest bit_count bits of x, so the least significant bit becomes the most significant bit (wi… |
bit.rol | Returns the number x rotated disp bits to the left. |
bit.ror | Returns the number x rotated disp bits to the right. |
bit.rshift | Returns the number x shifted disp bits to the right. |
bit.sign | Returns the sign of an integer. |
bit.tobit | Normalizes the specified value to a signed 32-bit integer by wrapping it around modulo 2^32, so values outside the rang… |
bit.tohex | Returns the lowercase hexadecimal representation of the number, zero-padded to length digits. |
bit.unsign | Returns the unsigned value of an integer. |
bit.width | 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.arshift
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.
1111 1000 -8 (as a signed 8-bit value)
ARSH 0000 0010 2
_____________
1111 1110 -2| Name | Type | Description |
|---|---|---|
x | T | The value to be manipulated. |
disp | integer | Amounts of bits to shift. |
| Type | Name | Description |
|---|---|---|
T | result | The arithmetically shifted value. |
#bit.band
function bit.band(x: T, ...?: T): T
Returns the bitwise AND of all provided values.
Each bit is tested against the following truth table:
| A | B | Output |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 1 | 1 |
1100 1110 222
0101 1000 88
AND 0100 1001 73
_____________
0100 1000 72| Name | Type | Description |
|---|---|---|
x | T | The value to be manipulated. |
...optional | T | Values bit to perform bitwise AND with. Optional. |
| Type | Name | Description |
|---|---|---|
T | result | The bitwise AND result between all values. |
#bit.bnot
function bit.bnot(x: T): T
Returns the bitwise NOT ( negation ) of x.
Inverts every bit of the 32-bit integer.
NOT 1111 0000 240
_____________
0000 1111 15<br/>
For any integer x, the following identity holds:
assert( bit.bnot( x ) == ( -1 - x ) % 2 ^ 32 )
````| Name | Type | Description |
|---|---|---|
x | T | The value to be manipulated/inverted. |
| Type | Name | Description |
|---|---|---|
T | result | The result of bitwise NOT (0101 becomes 1010). |
#bit.bor
function bit.bor(x: T, ...?: T): T
Returns the bitwise OR of all provided values.
Each bit is tested against the following truth table:
| A | B | Output |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 0 | 1 |
| 0 | 1 | 1 |
| 1 | 1 | 1 |
0101 0001 81
0001 0101 21
OR 0000 0101 5
_____________
0101 0101 85| Name | Type | Description |
|---|---|---|
x | T | The value to be manipulated. |
...optional | T | Values bit or with. Optional. |
| Type | Name | Description |
|---|---|---|
T | result | The bitwise OR result between all values. |
#bit.bswap
function bit.bswap(x: T): T
Swaps the byte order of a 32-bit integer.
| Name | Type | Description |
|---|---|---|
x | T | The 32-bit integer to be byte-swapped. |
| Type | Name | Description |
|---|---|---|
T | result | The byte-swapped value. |
#bit.btest
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.
| Name | Type | Description |
|---|---|---|
x | T | The first value to test. |
... | integer | Additional values to test against the first. Optional. |
| Type | Name | Description |
|---|---|---|
boolean | result | true if any bit is set in every given value, false otherwise. |
#bit.bxor
function bit.bxor(x: T, ...?: T): T
Returns the bitwise XOR of all provided numbers.
Each bit is tested against the following truth table:
| A | B | Output |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 0 | 1 |
| 0 | 1 | 1 |
| 1 | 1 | 0 |
<br/>
0101 0001 81
0001 0101 21
XOR 0000 0101 5
_____________
0100 0001 65| Name | Type | Description |
|---|---|---|
x | T | The value to be manipulated. |
...optional | T | Values bit XOR with. Optional. |
| Type | Name | Description |
|---|---|---|
T | result | Result of bitwise XOR` operation. |
#bit.clz
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).
| Name | Type | Description |
|---|---|---|
x | T | The value to count the leading zeros of. |
bit_countoptional | integer | The width, in bits, of the window to inspect. Defaults to 32. |
| Type | Name | Description |
|---|---|---|
T | result | The number of leading zero bits. |
#bit.ctz
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).
| Name | Type | Description |
|---|---|---|
x | T | The value to count the trailing zeros of. |
bit_countoptional | integer | The width, in bits, of the window to inspect. Defaults to 32. |
| Type | Name | Description |
|---|---|---|
T | result | The number of trailing zero bits. |
#bit.extract
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.
extract( 0b10110100, 2, 3 ) --> 0b101 ( 5 )| Name | Type | Description |
|---|---|---|
x | T | The value to extract the bit field from. |
field | integer | The starting bit position of the field (0-indexed). |
widthoptional | integer | The number of bits in the field. Defaults to 1. |
| Type | Name | Description |
|---|---|---|
T | result | The extracted bit field, right-aligned as an unsigned integer. |
#bit.lshift
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).
0000 1111 15
LSH 0000 0011 3
_____________
0111 1000 120NOTE: The returned value will be clamped to a signed 32-bit integer, even on 64-bit builds.
See this wiki article for more details.
| Name | Type | Description |
|---|---|---|
x | T | The value to be manipulated. |
disp | integer | Amounts of bits to shift left by. |
| Type | Name | Description |
|---|---|---|
T | result | The left shifted value. Input of 0b1001 will become 0b10010 for one left shift, etc. |
#bit.maj
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.
| Name | Type | Description |
|---|---|---|
a | T | The first value. |
b | T | The second value. |
c | T | The third value. |
| Type | Name | Description |
|---|---|---|
T | result | The bitwise majority value. |
#bit.mux
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.
| Name | Type | Description |
|---|---|---|
a | T | The selector value: a 1 bit picks from b, a 0 bit picks from c. |
b | T | The value to select bits from where a is 1. |
c | T | The value to select bits from where a is 0. |
| Type | Name | Description |
|---|---|---|
T | result | The bitwise multiplexed value. |
#bit.replace
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.
| Name | Type | Description |
|---|---|---|
x | T | The value whose bit field is to be replaced. |
extract | integer | The value providing the replacement bits (only the lowest width bits are used). |
field | integer | The starting bit position of the field to replace (0-indexed). |
widthoptional | integer | The number of bits in the field. Defaults to 1. |
| Type | Name | Description |
|---|---|---|
T | result | The value of x with the specified bit field replaced. |
#bit.reverse
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.
reverse( 0b1011000, 7 ) --> 0b0001101| Name | Type | Description |
|---|---|---|
x | T | The value whose bits are to be reversed. |
bit_countoptional | integer | The number of low-order bits to reverse. Defaults to 32. |
| Type | Name | Description |
|---|---|---|
T | result | The value of x with its bits reversed. |
#bit.rol
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:
assert( bit.lrotate( x, disp ) == bit.lrotate( x, disp % 32 ) )| Name | Type | Description |
|---|---|---|
x | T | The value to be manipulated. |
disp | integer | Amounts of bits to rotate left by. |
| Type | Name | Description |
|---|---|---|
T | result | The left rotated value. |
#bit.ror
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:
assert( bit.rrotate( x, disp ) == bit.rrotate( x, disp % 32 ) )In particular, negative displacements rotate to the left.
| Name | Type | Description |
|---|---|---|
x | T | The value to be manipulated. |
disp | integer | Amounts of bits to rotate right by. |
| Type | Name | Description |
|---|---|---|
T | result | The right rotated value. |
#bit.rshift
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).
0111 1000 120
RSH 0000 0011 3
_____________
0000 1111 15NOTE: The returned value will be clamped to a signed 32-bit integer, even on 64-bit builds.
See this wiki article for more details.
| Name | Type | Description |
|---|---|---|
x | T | The value to be manipulated. |
disp | integer | Amounts of bits to shift right by. |
| Type | Name | Description |
|---|---|---|
T | result | The right shifted value. |
#bit.sign
function bit.sign(x: T, bit_count?: integer): T
Returns the sign of an integer.
| Name | Type | Description |
|---|---|---|
x | T | The integer to get the sign of. |
bit_countoptional | integer | The amount of bits to unsign to, 32 by default. |
| Type | Name | Description |
|---|---|---|
T | result | The sign of the integer: 1 for positive, 0 for zero, -1 for negative. |
#bit.tobit
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.
rbit.tobit( 0xFFFFFFFF ) -- -1
rbit.tobit( 2 ^ 32 + 5 ) -- 5| Name | Type | Description |
|---|---|---|
x | (integer | ffi.cdata*) | The value to be normalized. |
| Type | Name | Description |
|---|---|---|
integer | result | The normalized value, in the range -2147483648 to 2147483647. |
#bit.tohex
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.
rbit.tohex( 255 ) -- "000000ff"
rbit.tohex( 255, 2 ) -- "ff"| Name | Type | Description |
|---|---|---|
x | (integer | ffi.cdata*) | The value to be converted. |
lengthoptional | integer | The number of digits, must be positive. Defaults to 8. |
| Type | Name | Description |
|---|---|---|
string | str | The hexadecimal representation. |
#bit.unsign
function bit.unsign(x: T, bit_count?: integer): T
Returns the unsigned value of an integer.
| Name | Type | Description |
|---|---|---|
x | T | The integer to get the unsigned value of. |
bit_countoptional | integer | The amount of bits to unsign to, 32 by default. |
| Type | Name | Description |
|---|---|---|
T | result | The unsigned value of the integer. |
#bit.width
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.
bit.width( 0 ) --> 0
bit.width( 255 ) --> 8
bit.width( 256 ) --> 9| Name | Type | Description |
|---|---|---|
x | T | The value to measure. |
bit_countoptional | integer | The width, in bits, of the window to inspect. Defaults to 32. |
| Type | Name | Description |
|---|---|---|
integer | width | The number of significant bits, from 0 to bit_count. |