Lua 5.4 bitwise operation precision on 64-bit integers
0 reputation · 24 Dec 2020, 16:44 UTC
64-bit integer emulation in Lua 5.4
Lua 5.4 introduced bitwise operators that operate on values interpreted as signed 64-bit integers. However, the Lua number type is typically a double-precision float with a 53-bit mantissa, creating a potential conflict in how the interpreter handles values exceeding this threshold during bitwise transformations.
When performing bitwise shifts or logic on integers larger than 2^53, the implementation relies on two-complement emulation to simulate 64-bit integer behavior. Because the underlying double cannot precisely represent every 64-bit integer, there is a risk of precision loss or unexpected overflow behavior when the result is converted back to a number type for further arithmetic or storage.
This behavior may differ across Lua implementations, such as LuaJIT 2.1, which may not preserve the full 64-bit range or may truncate to 53-bit mantissa precision.
- Does Lua 5.4 guarantee full 64-bit precision for bitwise operations when the input exceeds the 53-bit mantissa limit?
- How does the behavior differ when a bitwise operation results in a value that cannot be exactly represented as a double-precision float?
- What are the measurable differences between standard Lua 5.4 and LuaJIT 2.1 when performing bitwise operations on values near the 64-bit boundary?