Handling 64-bit Integers


64-bit Integers

Lua numbers are floating points (doubles) internally, not integers; thus while they can represent incredibly large numbers, above 2^53 they lose integral precision - they can't represent every whole integer value. For example if you set a lua variable to the number 9007199254740992 and tried to increment it by 1, you'd get the same number because it can't represent 9007199254740993 (only the even number 9007199254740994).

Therefore, in order to count higher than 2^53 in integers, we need a true integer type. The way this is done is with an explicit 'Int64' or 'UInt64' object (i.e., Lua userdata). This object has metamethods for all of the math and comparison operators, so you can handle it like any number variable. For the math operators, it can even be mixed with plain Lua numbers.

For example 'my64num = my64num + 1' will work even if 'my64num' is a Int64 or UInt64 object. Note that comparison operators ('==','<=','>',etc.) will not work with plain numbers - only other Int64/UInt64 objects. This is a limitation of Lua itself, in terms of how it handles operator overloading.

{i} Previous to Wireshark release 1.11, Int64 and UInt64 could only be created by tvbrange:int64() or tvbrange:le_int64(), or tvbrange:uint64() or tvbrange:le_uint64() or tvbrange:bitfield(), and had only a couple functions (the metamethods tostring() and concat()). All of the functions on this page are only available starting in Wireshark 1.11 and higher.

/!\ Many of the UInt64/Int64 functions accept a Lua number as an argument. You should be very careful to never use Lua numbers bigger than 32-bits (i.e., the number value 4,294,967,295 or the literal 0xFFFFFFFF) for such arguments, because Lua itself does not handle bigger numbers consistently across platforms (32-bit vs. 64-bit systems), and because a Lua number is a C-code double which cannot have more than 53 bits of precision. Instead, use a Int64 or UInt64 for the argument.

For example, do this:

   1 local mynum = UInt64(0x2b89dd1e, 0x3f91df0b)

...instead of this:

   1 -- bad way, leads to inconsistent results across platforms
   2 local mynum = UInt64(0x3f91df0b2b89dd1e)

And do this:

   1 local masked = mynum:band(UInt64(0, 0xFFFFFFFF))

...instead of this:

   1 -- bad way, leads to inconsistent results across platforms
   2 local masked = mynum:band(0xFFFFFFFF00000000)


Int64

An Int64 represents a signed 64-bit integer.

function

description

Int64.new([num [,high_num)]]

Creates an Int64 object

Int64.max()

Creates an Int64 of the max value

Int64.min()

Creates an Int64 of the min value

Int64.decode(string [,endian])

Decodes an 8-byte Lua string to an Int64

int64:encode([endian])

Encodes the Int64 into an 8-byte Lua string

Int64.fromhex(hex)

Creates an Int64 from a hex string

int64:tohex([numbytes])

Gets a hex string from a Int64

int64:tonumber()

Gets a Lua number of the Int64 value

int64:higher()

Gets the high 32-bit number of the Int64 value

int64:lower()

Gets the lower 32-bit number of the Int64 value

int64:bnot()

Gets the bitwise 'not' of the Int64 value

int64:band(arg1 [, arg2 [, ...] ])

Gets the bitwise 'and' of the Int64 values

int64:bor(arg1 [, arg2 [, ...] ])

Gets the bitwise 'or' of the Int64 values

int64:bxor(arg1 [, arg2 [, ...] ])

Gets the bitwise 'xor' of the Int64 values

int64:lshift(numbits)

Gets the bitwise logical left-shift the Int64 value

int64:rshift(numbits)

Gets the bitwise logical right-shift the Int64 value

int64:arshift(numbits)

Gets the bitwise arithmetic right-shift the Int64 value

int64:rol(numbits)

Gets the bitwise left rotation of the Int64 value

int64:ror(numbits)

Gets the bitwise right rotation of the Int64 value

int64:swap()

Gets the byte swap of the Int64 value

metamethod

calling function

description

Int64.__tostring()

tostring()

Gets a digit string of the Int64 value

Int64.__unm()

-

Gets the negative of the Int64 value

Int64.__add()

+

Gets the addition of the Int64 with another Int64/UInt64/number

Int64.__sub()

-

Gets the subtraction of the Int64 with another Int64/UInt64/number

Int64.__mul()

*

Gets the multiplication of the Int64 with another Int64/UInt64/number

Int64.__div()

/

Gets the integer division of the Int64 with another Int64/UInt64/number

Int64.__mod()

%

Gets the remainder of division of the Int64 with another Int64/UInt64/number

Int64.__pow()

^

Gets the power of the Int64 to another Int64/UInt64/number

Int64.__eq()

==

Returns true if the Int64 equals another Int64/UInt64/number

Int64.__lt()

<

Returns true if the Int64 is less than another Int64/UInt64/number

Int64.__le()

<=

Returns true if the Int64 is less than or equal to another Int64/UInt64/number


Int64.new([num [,high_num]])


Int64.max()


Int64.min()


Int64.decode(string [,endian])


int64:encode([endian])


Int64.fromhex(hex)


int64:tohex([numbytes])


int64:tonumber()


int64:higher()


int64:lower()


int64:bnot()


int64:band(arg1 [, arg2 [, ...]])


int64:bor(arg1 [, arg2 [, ...]])


int64:bxor(arg1 [, arg2 [, ...]])


int64:lshift(numbits)


int64:rshift(numbits)


int64:arshift(numbits)


int64:rol(numbits)


int64:ror(numbits)


int64:bswap()


Int64 metamethods


Int64.__tostring()


Int64.__unm()


Int64.__add()


Int64.__sub()


Int64.__mul()


Int64.__div()


Int64.__mod()


Int64.__pow()


Int64.__eq()


Int64.__lt()


Int64.__le()


UInt64

A UInt64 represents a 64-bit unsigned integer.

function

description

UInt64.new([num [,high_num)]]

Creates an UInt64 object

UInt64.max()

Creates an UInt64 of the max value

UInt64.min()

Creates an UInt64 of the min value

UInt64.decode(string [,endian])

Decodes an 8-byte Lua string to an UInt64

uint64:encode([endian])

Encodes the UInt64 into an 8-byte Lua string

UInt64.fromhex(hex)

Creates an UInt64 from a hex string

uint64:tohex([numbytes])

Gets a hex string from a UInt64

uint64:tonumber()

Gets a Lua number of the UInt64 value

uint64:higher()

Gets the high 32-bit number of the UInt64 value

uint64:lower()

Gets the lower 32-bit number of the UInt64 value

uint64:bnot()

Gets the bitwise 'not' of the UInt64 value

uint64:band(arg1 [, arg2 [, ...] ])

Gets the bitwise 'and' of the UInt64 values

uint64:bor(arg1 [, arg2 [, ...] ])

Gets the bitwise 'or' of the UInt64 values

uint64:bxor(arg1 [, arg2 [, ...] ])

Gets the bitwise 'xor' of the UInt64 values

uint64:lshift(numbits)

Gets the bitwise logical left-shift the UInt64 value

uint64:rshift(numbits)

Gets the bitwise logical right-shift the UInt64 value

uint64:arshift(numbits)

Gets the bitwise arithmetic right-shift the UInt64 value

uint64:rol(numbits)

Gets the bitwise left rotation of the UInt64 value

uint64:ror(numbits)

Gets the bitwise right rotation of the UInt64 value

uint64:swap()

Gets the byte swap of the UInt64 value

metamethod

calling function

description

UInt64.__tostring()

tostring()

Gets a digit string of the UInt64 value

UInt64.__unm()

-

Gets the negative of the UInt64 value

UInt64.__add()

+

Gets the addition of the UInt64 with another Int64/UInt64/number

UInt64.__sub()

-

Gets the subtraction of the UInt64 with another Int64/UInt64/number

UInt64.__mul()

*

Gets the multiplication of the UInt64 with another Int64/UInt64/number

UInt64.__div()

/

Gets the integer division of the UInt64 with another Int64/UInt64/number

UInt64.__mod()

%

Gets the remainder of division of the UInt64 with another Int64/UInt64/number

UInt64.__pow()

^

Gets the power of the UInt64 to another Int64/UInt64/number

UInt64.__eq()

==

Returns true if the UInt64 equals another Int64/UInt64/number

UInt64.__lt()

<

Returns true if the UInt64 is less than another Int64/UInt64/number

UInt64.__le()

<=

Returns true if the UInt64 is less than or equal to another Int64/UInt64/number


UInt64.new([num [,high_num]])


UInt64.max()


UInt64.min()


UInt64.decode(string [,endian])


uint64:encode([endian])


UInt64.fromhex(hex)


uint64:tohex([numbytes])


uint64:tonumber()


uint64:higher()


uint64:lower()


uint64:bnot()


uint64:band(arg1 [, arg2 [, ...]])


uint64:bor(arg1 [, arg2 [, ...]])


uint64:bxor(arg1 [, arg2 [, ...]])


uint64:lshift(numbits)


uint64:rshift(numbits)


uint64:arshift(numbits)


uint64:rol(numbits)


uint64:ror(numbits)


uint64:bswap()


UInt64 metamethods


UInt64.__tostring()


UInt64.__unm()


UInt64.__add()


UInt64.__sub()


UInt64.__mul()


UInt64.__div()


UInt64.__mod()


UInt64.__pow()


UInt64.__eq()


UInt64.__lt()


UInt64.__le()

LuaAPI/Int64 (last edited 2014-03-21 18:35:42 by HadrielKaplan)