clarity the examples assume the definition of a helper function
`printx()`. This prints its argument as an unsigned 32 bit hexadecimal number
on all platforms:
>lua
function printx(x)
print("0x"..bit.tohex(x))
end
<
------------------------------------------------------------------------------
Bit operations
*lua-bitop*
y = bit.tobit(x) *bit.tobit()*
Normalizes a number to the numeric range for bit operations and returns
it. This function is usually not needed since all bit operations already
normalize all of their input arguments. See |lua-bit-semantics|.
Example: >lua
print(0xffffffff) --> 4294967295 (see Note)
print(bit.tobit(0xffffffff)) --> -1
printx(bit.tobit(0xffffffff)) --> 0xffffffff
print(bit.tobit(0xffffffff + 1)) --> 0
print(bit.tobit(2^40 + 1234)) --> 1234
<
Note: |lua-bit-hex-literals| explains why the numbers printed in the first
two lines differ (if your Lua installation uses a double number type).
y = bit.tohex(x [,n]) *bit.tohex()*
Converts its first argument to a hex string. The number of hex digits is
given by the absolute value of the optional second argument. Positive
numbers between 1 and 8 generate lowercase hex digits. Negative numbers
generate uppercase hex digits. Only the least-significant `4*|n|` bits are
used. The default is to generate 8 lowercase hex digits.
Example: >lua
print(bit.tohex(1)) --> 00000001
print(bit.tohex(-1)) --> ffffffff
print(bit.tohex(0xffffffff)) --> ffffffff
print(bit.tohex(-1, -8)) --> FFFFFFFF
print(bit.tohex(0x21, 4)) --> 0021
print(bit.tohex(0x87654321, 4)) --> 4321
<
y = bit.bnot(x) *bit.bnot()*
Returns the bitwise `not` of its argument.
Example: >lua
print(bit.bnot(0)) --> -1
printx(bit.bnot(0)) --> 0xffffffff
print(bit.bnot(-1)) --> 0
print(bit.bnot(0xffffffff)) --> 0
printx(bit.bnot(0x12345678)) --> 0xedcba987
<
y = bit.bor(x1 [,x2...]) *bit.bor()*
y = bit.band(x1 [,x2...]) *bit.band()*
y = bit.bxor(x1 [,x2...]) *bit.bxor()*
Returns either the bitwise `or`, bitwise `and`, or bitwise `xor` of all of its
arguments. Note that more than two arguments are allowed.
Example: >lua
print(bit.bor(1, 2, 4, 8)) --> 15
printx(bit.band(0x12345678, 0xff)) --> 0x00000078
printx(bit.bxor(0xa5a5f0f0, 0xaa55ff00)) --> 0x0ff00ff0
<
y = bit.lshift(x, n) *bit.lshift()*
y = bit.rshift(x, n) *bit.rshift()*
y = bit.arshift(x, n) *bit.arshift()*
Returns either the bitwise `logical left-shift`, bitwise `logical`
`right-shift`, or bitwise `arithmetic right-shift` of its first argument
by the number of bits given by the second argument.
Logical shifts treat the first argument as an unsigned number and shift in
0-bits. Arithmetic right-shift treats the most-significant bit as a sign
bit and replicates it. Only the lower 5 bits of the shift count are used
(reduces to the range [0..31]).
Example: >lua
print(bit.lshift(1, 0)) --> 1
print(bit.lshift(1, 8)) --> 256
print(bit.lshift(1, 40)) --> 256
print(bit.rshift(256, 8)) --> 1
print(bit.rshift(-256, 8)) --> 16777215
print(bit.arshift(256, 8)) --> 1
print(bit.arshift(-256, 8)) --> -1