Library bit
Bitwise operations on integers.
Lua does not provide bitwise logical operations. Since they are often useful for low-level network communication, Reuben Thomas' BitLib (http://luaforge.net/projects/bitlib) for Lua has been integrated into NSE. The arguments to the bitwise operation functions should be integers. The number of bits available for logical operations depends on the data type used to represent Lua numbers. This is typically 8-byte IEEE floats (double), which give 53 bits (the size of the mantissa).
This implies that the bitwise operations won't work (as expected) for numbers larger than 10^14. You can use them with 32-bit wide numbers without any problems. Operations involving 64-bit wide numbers, however, may not return the expected result.
The logical operations start with "b" to avoid
clashing with reserved words; although xor isn't a
reserved word, it seemed better to use bxor for
consistency.
Author:
| Reuben Thomas |
Copyright© BSD License
Functions
| arshift (a, b) |
Returns |
| band (...) |
Returns the bitwise and of all its arguments. |
| bnot (a) |
Returns the one's complement of |
| bor (...) |
Returns the bitwise or of all its arguments. |
| bxor (...) |
Returns the bitwise exclusive or of all its arguments. |
| lshift (a, b) |
Returns |
| mod (a, b) |
Returns the integer remainder of |
| rshift (a, b) |
Returns |
Functions
- arshift (a, b)
-
Returns
aarithmetically right-shifted bybplaces.Parameters
- a: Number to perform the shift on.
- b: Number of shifts.
- band (...)
-
Returns the bitwise and of all its arguments.
Parameters
- ...: A variable number of Numbers to and.
Return value:
The anded result. - bnot (a)
-
Returns the one's complement of
a.Parameters
- a: Number.
Return value:
The one's complement ofa. - bor (...)
-
Returns the bitwise or of all its arguments.
Parameters
- ...: A variable number of Numbers to or.
Return value:
The ored result. - bxor (...)
-
Returns the bitwise exclusive or of all its arguments.
Parameters
- ...: A variable number of Numbers to exclusive or.
Return value:
The exclusive ored result. - lshift (a, b)
-
Returns
aleft-shifted bybplaces.Parameters
- a: Number to perform the shift on.
- b: Number of shifts.
- mod (a, b)
-
Returns the integer remainder of
adivided byb.Parameters
- a: Dividend.
- b: Divisor.
- rshift (a, b)
-
Returns
aright-shifted bybplaces.Parameters
- a: Number to perform the shift on.
- b: Number of shifts.


