<menu id="guoca"></menu>
<nav id="guoca"></nav><xmp id="guoca">
  • <xmp id="guoca">
  • <nav id="guoca"><code id="guoca"></code></nav>
  • <nav id="guoca"><code id="guoca"></code></nav>

    處理64位整數

    Lua使用一個可以在編譯時選擇的單數字表示,由于它經常被設置為ieee754雙精度浮點數,因此無法存儲完全精確的64位整數。

    Lua數字內部存儲為浮點數(雙精度數),而不是整數;
    因此,雖然它們可以代表難以置信的大數字,超過2^53,它們就失去了積分精度——它們不能代表每一個整數值。
    例如,如果您將lua變量設置為數字9007199254740992,并嘗試將其加1,您將得到相同的數字,因為它不能表示9007199254740993(只有偶數9007199254740994)。

    因此,為了在整數中計數大于2^53,我們需要一個真正的整數類型。
    這是通過一個顯式的’Int64’或’UInt64’對象來完成的。
    Lua用戶數據)。
    該對象對所有數學和比較運算符都有元方法,因此可以像處理任何數字變量一樣處理它。
    對于數學運算符,它甚至可以與普通Lua數字混合。

    例如,’my64num = my64num + 1’將工作,即使’my64num’是一個Int64或UInt64對象。
    注意,比較運算符(‘==’、’<=’、’>’等)不適用于純數字—只適用于其他Int64/UInt64對象。
    這是Lua本身在處理操作符重載方面的一個限制。

        local mynum = UInt64(0x2b89dd1e, 0x3f91df0b)
    …?instead of this:
        -- Bad. Leads to inconsistent results across platforms
        local mynum = UInt64(0x3f91df0b2b89dd1e)
    And do this…?
        local masked = mynum:band(UInt64(0, 0xFFFFFFFF))
    …?instead of this:
        -- Bad. Leads to inconsistent results across platforms
        local masked = mynum:band(0xFFFFFFFF00000000)

    Int64

    Int64represents a 64 bit signed integer.

    Note the caveats listed above.

    Int64.decode(string, [endian])

    Decodes an 8-byte Lua string, using the given endianness, into a new Int64 object.

    Arguments

    string

    The Lua string containing a binary 64-bit integer.

    endian (optional)

    If set to true then little-endian is used, if false then big-endian; if missing or nil, native host endian.

    Returns

    The Int64 object created, or nil on failure.

    Int64.new([value], [highvalue])

    Creates a Int64 Object.

    Arguments

    value (optional)

    一個數字’ UInt64 ‘, ‘ Int64 ‘,或一串ASCII數字,用于分配新的’ Int64 ‘的值,默認值為0。

    highvalue (optional)

    如果這是一個數,而第一個參數也是一個數,那么第一個將被視為較低的32位,而這是高階的32位數。

    Returns

    The new Int64 object.

    Int64.max()

    Creates an Int64 of the maximum possible positive value. In other words, this should return an Int64 object of the number 9,223,372,036,854,775,807.

    Returns

    The new Int64 object of the maximum value.

    Int64.min()

    Creates an Int64 of the minimum possible negative value. In other words, this should return an Int64 object of the number -9,223,372,036,854,775,808.

    Returns

    The new Int64 object of the minimum value.

    Int64.fromhex(hex)

    從給定的十六進制字符串創建一個Int64對象。

    Arguments

    hex

    The hex-ASCII Lua string.

    Returns

    The new Int64 object.

    int64:encode([endian])

    使用給定的endianness將Int64編碼為8字節的Lua字符串。

    Arguments

    endian (optional)

    If set to true then little-endian is used, if false then big-endian; if missing or nil, native host endian.

    Returns

    The Lua string.

    int64:__call()

    Creates a Int64 object.

    Returns

    The new Int64 object.

    int64:tonumber()

    返回Int64值的Lua值。
    注意,這可能會失去精度。

    Returns

    The Lua number.

    int64:tohex([numbytes])

    Returns a hexadecimal string of the Int64 value.

    Arguments

    numbytes (optional)

    The number of hex chars/nibbles to generate. A negative value generates uppercase. Default is 16.

    Returns

    The string hex.

    int64:higher()

    返回’ Int64 ‘值中較高的32位的Lua數,一個負的’ Int64 ‘將返回一個負的Lua數字。

    int64:lower()

    返回Int64值較低的32位的Lua數。
    它總是正的。

    Returns

    The Lua number.

    int64:__tostring()

    Converts the Int64 into a string of decimal digits.

    Returns

    The Lua string.

    nt64:__unm()

    Returns the negative of the Int64 as a new Int64.

    Returns

    The new Int64.

    int64:__add()

    Adds two Int64 together and returns a new one. The value may wrapped.

    int64:__sub()

    Subtracts two Int64 and returns a new one. The value may wrapped.

    int64:__mul()

    Multiplies two Int64 and returns a new one. The value may truncated.

    int64:__div()

    Divides two Int64 and returns a new one. Integer divide, no remainder. Trying to divide by zero results in a Lua error.

    Returns

    The Int64 object.

    int64:__mod()

    Divides two Int64 and returns a new one of the remainder. Trying to modulo by zero results in a Lua error.

    Returns

    The Int64 object.

    int64:__pow()

    The first Int64 is taken to the power of the second Int64, returning a new one. This may truncate the value.

    Returns

    The Int64 object.

    int64:__eq()

    Returns true if both Int64 are equal.

    int64:__lt()

    Returns true if first Int64 is less than the second.

    int64:__le()

    Returns true if the first Int64 is less than or equal to the second.

    int64:bnot()

    Returns a Int64 of the bitwise ‘not’ operation.

    Returns

    The Int64 object.

    int64:band()

    Returns a Int64 of the bitwise ‘and’ operation with the given number/Int64/UInt64. Note that multiple arguments are allowed.

    int64:bor()

    Returns a Int64 of the bitwise ‘or’ operation, with the given number/Int64/UInt64. Note that multiple arguments are allowed.

    int64:bxor()

    Returns a Int64 of the bitwise ‘xor’ operation, with the given number/Int64/UInt64. Note that multiple arguments are allowed.

    int64:lshift(numbits)

    Returns a Int64 of the bitwise logical left-shift operation, by the given number of bits.

    Arguments

    numbits

    The number of bits to left-shift by.

    Returns

    The Int64 object.

    int64:rshift(numbits)

    Returns a Int64 of the bitwise logical right-shift operation, by the given number of bits.

    Arguments

    numbits

    The number of bits to right-shift by.

    Returns

    The Int64 object.

    int64:arshift(numbits)

    Returns a Int64 of the bitwise arithmetic right-shift operation, by the given number of bits.

    Arguments

    numbits

    The number of bits to right-shift by.

    Returns

    The Int64 object.

    int64:rol(numbits)

    Returns a Int64 of the bitwise left rotation operation, by the given number of bits (up to 63).

    Arguments

    numbits

    The number of bits to roll left by.

    Returns

    The Int64 object.

    int64:ror(numbits)

    Returns a Int64 of the bitwise right rotation operation, by the given number of bits (up to 63).

    Arguments

    numbits

    The number of bits to roll right by.

    Returns

    The Int64 object.

    int64:bswap()

    Returns a Int64 of the bytes swapped. This can be used to convert little-endian 64-bit numbers to big-endian 64 bit numbers or vice versa.

    Returns

    The Int64 object.

    本文章首發在 網安wangan.com 網站上。

    上一篇 下一篇
    討論數量: 0
    只看當前版本


    暫無話題~
    亚洲 欧美 自拍 唯美 另类