Home Explore Blog CI



neovim

5th chunk of `runtime/doc/lua-bit.txt`
ddf104912fffaa67aabf6ddbddd6087963af98cfeec947d100000001000009d2
 numbers (or their
underlying bit patterns). They must be converted to integers before operating
on them and then back to FP numbers.

It's desirable to define semantics that work the same across all platforms.
This dictates that all operations are based on the common denominator of 32
bit integers. The `float` type provides only 24 bits of precision. This makes it
unsuitable for use in bitwise operations. Lua BitOp refuses to compile against
a Lua installation with this number type.

Bit operations only deal with the underlying bit patterns and generally ignore
signedness (except for arithmetic right-shift). They are commonly displayed
and treated like unsigned numbers, though.

But the Lua number type must be signed and may be limited to 32 bits. Defining
the result type as an unsigned number would not be cross-platform safe. All
bit operations are thus defined to return results in the range of signed 32
bit numbers (converted to the Lua number type).

                                                        *lua-bit-hex-literals*
Hexadecimal literals are treated as unsigned numbers by the Lua parser before
converting them to the Lua number type. This means they can be out of the
range of signed 32 bit integers if the Lua number type has a greater range.
E.g. 0xffffffff has a value of 4294967295 in the default installation, but may
be -1 on embedded systems. It's highly desirable that hex literals are treated
uniformly across systems when used in bitwise operations. All bit operations
accept arguments in the signed or the unsigned 32 bit range (and more, see
below). Numbers with the same underlying bit pattern are treated the same by
all operations.


Modular Arithmetic ~
                                                        *lua-bit-modular-arith*

Arithmetic operations on n-bit integers are usually based on the rules of
modular arithmetic modulo 2^n. Numbers wrap around when the mathematical result
of operations is outside their defined range. This simplifies hardware
implementations and some algorithms actually require this behavior (like many
cryptographic functions).

E.g. for 32 bit integers the following holds: `0xffffffff + 1 = 0`

Arithmetic modulo 2^32 is trivially available if the Lua number type is a 32
bit integer. Otherwise normalization steps must be inserted. Modular
arithmetic should work the same across all platforms as far as possible:

- For the default number type of double, arguments can be in the range of
  ±2^51 and still be safely normalized

Title: Lua BitOp: Hex Literals and Modular Arithmetic
Summary
This section explains how Lua BitOp handles hexadecimal literals, emphasizing that they are treated as unsigned numbers by the Lua parser, which can lead to inconsistencies across systems if the Lua number type has a greater range than signed 32-bit integers. It also discusses modular arithmetic, a key concept in bitwise operations where numbers wrap around when the result of an operation exceeds the defined range, and how Lua BitOp aims to ensure consistent modular arithmetic behavior across different platforms.