Module bit32
This library provides bitwise operations.
The library is taken from Lua 5.2, and this documentation also reuses Lua 5.2 Bitwise Operations documentation.
Unless otherwise stated, all functions accept numeric arguments in the range (-251,+251); each argument is normalized to the remainder of its division by 232 and truncated to an integer (in some unspecified way), so that its final value falls in the range [0,232 - 1]. Similarly, all results are in the range [0,232 - 1]. Note that bit32.bnot(0) is 0xFFFFFFFF, which is different from -1.
Type bit32
bit32.arshift(x, disp) |
Returns the number |
bit32.band(...) |
Returns the bitwise and of its operands. |
bit32.bnot(x) |
Returns the bitwise negation of |
bit32.bor(...) |
Returns the bitwise or of its operands. |
bit32.btest(...) |
Returns a boolean signaling whether the bitwise and of its operands is different from zero. |
bit32.bxor(...) |
Returns the bitwise exclusive or of its operands. |
bit32.extract(n, field, width) |
Returns the unsigned number formed by the bits |
bit32.lrotate(x, disp) |
Returns the number |
bit32.lshift(x, disp) |
Returns the number |
bit32.replace(n, v, field, width) |
Returns a copy of |
bit32.rrotate(x, disp) |
Returns the number |
bit32.rshift(x, disp) |
Returns the number |
Type bit32
Field(s)
- bit32.arshift(x, disp)
-
Returns the number
x
shifteddisp
bits to the right.The number
disp
may be any representable integer. Negative displacements shift to the left. This shift operation is what is called arithmetic shift. Vacant bits on the left are filled with copies of the higher bit ofx
; vacant bits on the right are filled with zeros. In particular, displacements with absolute values higher than 31 result in zero or 0xFFFFFFFF (all original bits are shifted out).Parameters
-
x
: the number that will be shifted -
disp
: the number of bits to be shifted to the right
Return value
the number resulting of the operation.
Usage:
bit32.arshift(0x00F0000F, 2) --> 0x3C0003
-
- bit32.band(...)
-
Returns the bitwise and of its operands.
Parameter
-
...
: variable number of arguments to operate the bitwise and on, each argument must be a number.
Return value
the number resulting of the operation.
Usage:
bit32.band(0xAFF, 0xFAF, 0xFFA) --> 0xAAA
-
- bit32.bnot(x)
-
Returns the bitwise negation of
x
.For any integer
x
, the following identity holds:assert(bit32.bnot(x) == (-1 - x) % 2^32)
Parameter
-
x
: number to return the bitwise negation of.
Return value
the number resulting of the operation.
Usage:
bit32.bnot(0x00F0000F) --> 0xFF0FFFF0
-
- bit32.bor(...)
-
Returns the bitwise or of its operands.
Parameter
-
...
: variable number of arguments to operate the bitwise or on, each argument must be a number.
Return value
the number resulting of the operation.
Usage:
bit32.bor(0xA, 0xA0, 0xA00) --> 0xAAA
-
- bit32.btest(...)
-
Returns a boolean signaling whether the bitwise and of its operands is different from zero.
Parameter
-
...
: variable number of arguments to test, each argument must be a number.
Return value
a boolean signaling whether the bitwise and of the operands is different from zero.
Usages:
bit32.btest(42, 1) --> false
bit32.btest(43, 1) --> true
-
- bit32.bxor(...)
-
Returns the bitwise exclusive or of its operands.
Parameter
-
...
: variable number of arguments to operate the bitwise exclusive or on , each argument must be a number.
Return value
the number resulting of the operation.
Usage:
bit32.bxor(0, 0xBBB, 0) --> 0xBBB
-
- bit32.extract(n, field, width)
-
Returns the unsigned number formed by the bits
field
tofield
+width
- 1 fromn
.Bits are numbered from 0 (least significant) to 31 (most significant). All accessed bits must be in the range [0, 31]. The default for
width
is 1.Parameters
-
n
: the number to extract bits from -
field
: the first bit to extract -
width
: optional number, number of bits to extract
Return value
the unsigned number resulting of the operation.
Usage:
bit32.extract(0xFEA8C523, 8, 8) --> 0xC5
-
- bit32.lrotate(x, disp)
-
Returns the number
x
rotateddisp
bits to the left.The number
disp
may be any representable integer. For any valid displacement, the following identity holds:assert(bit32.lrotate(x, disp) == bit32.lrotate(x, disp % 32))
In particular, negative displacements rotate to the right.
Parameters
-
x
: the number that will be rotated -
disp
: the number of bits to be rotated
Return value
the number resulting of the operation.
Usage:
bit32.lrotate(0x12345678, 8) --> 0x34567812
-
- bit32.lshift(x, disp)
-
Returns the number
x
shifteddisp
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).For positive displacements, the following equality holds:
assert(bit32.lshift(b, disp) == (b * 2^disp) % 2^32)
Parameters
-
x
: the number that will be shifted -
disp
: the number of bits to be shifted
Return value
the number resulting of the operation.
Usage:
bit32.lshift(0x12345678, 8) --> 0x34567800
-
- bit32.replace(n, v, field, width)
-
Returns a copy of
n
with the bitsfield
tofield
+width
- 1 replaced by the valuev
.See bit32.extract for details about field and width.
Parameters
-
n
: the number to replace bits in. -
v
: the value used to replace bits inn
. -
field
: the first bit to replace -
width
: optional number, number of bits to replace
Return value
the number resulting of the operation.
Usage:
bit32.replace(0xFEA8C523, 0x42, 8, 8) --> 0xFEA84223
-
- bit32.rrotate(x, disp)
-
Returns the number
x
rotateddisp
bits to the right.The number
disp
may be any representable integer. For any valid displacement, the following identity holds:assert(bit32.rrotate(x, disp) == bit32.rrotate(x, disp % 32))
In particular, negative displacements rotate to the left.
Parameters
-
x
: the number that will be rotated -
disp
: the number of bits to be rotated
Return value
the number resulting of the operation.
-
- bit32.rshift(x, disp)
-
Returns the number
x
shifteddisp
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).For positive displacements, the following equality holds:
assert(bit32.rshift(b, disp) == math.floor(b % 2^32 / 2^disp))
This shift operation is what is called logical shift.
Parameters
-
x
: the number that will be shifted -
disp
: the number of bits to be shifted to the right
Return value
the number resulting of the operation.
Usage:
bit32.rshift(0x12345678, 8) --> 0x123456
-