bit64

LuaFAR 3

bit64


As Far 3.0 started to use 64-bit integer values (“flags”) in its API, arose the need to represent and handle them from Lua. This is the role of bit64 library.

The library represents and accepts flags as either numbers or “64 bit integer” userdata. Values that fit in 53 bits can be represented in both ways. Greater values are always represented as userdata.

Userdata values created by the library support the following operations:
+, -, *, /, %, ==, <, <=, unary minus.

__tostring metamethod of “64-bit type” userdata returns an acceptable string representation that when passed to new will produce a value equal to the original one.

The library has the following functions:

  • bnot – gets a flag, returns a flag
  • band – gets multiple flags, returns a flag
  • bor – gets multiple flags, returns a flag
  • bxor – gets multiple flags, returns a flag
  • lshift – gets a flag and an integer, returns a flag
  • rshift – gets a flag and an integer, returns a flag
  • arshift – gets a flag and an integer, returns a flag
  • add – gets multiple flags, returns a flag
  • mul – gets multiple flags, returns a flag
  • sub – gets 2 flags, returns a flag
  • div – gets 2 flags, returns a flag
  • mod – gets 2 flags, returns a flag

The functions above always return a number if the result fits into 53 bits.

  • eq – gets 2 flags, returns a boolean
  • lt – gets 2 flags, returns a boolean
  • le – gets 2 flags, returns a boolean

The functions above are useful for comparisons where each operand may be either a “64 bit integer” type userdata or a number. They compare underlying 64-bit integers.

  • new – gets a string, number or flag representation, returns a flag
  • type – gets a value, returns a string (or nil)