Lua BitOps: Difference between revisions

From Spring
Jump to navigationJump to search
Jk (talk | contribs)
Zwzsg (talk | contribs)
No edit summary
Line 17: Line 17:
   ( int a1 [, int a2 [, ... ] ] ) -> int i
   ( int a1 [, int a2 [, ... ] ] ) -> int i


-- Note: there are no bit shift. Use those Lua functions instead for 24 bits bitshift
-- 24 bits because only the 24 bits of the mantissa can be easily used in a 32 bit float
-- bitshift functions (<<, >> equivalent)
-- shift left
local function lsh(value,shift)
    return (value*(2^shift)) % 2^24
end
-- shift right
local function rsh(value,shift)
    return math.floor(value/2^shift) % 2^24
end
[[category: Lua]]
[[category: Lua]]

Revision as of 23:59, 28 June 2013

Development < Lua Scripting < Lua BitOps

Bit Operators

math.bit_and

 ( int a1 [, int a2 [, ... ] ] ) -> int i

math.bit_or

 ( int a1 [, int a2 [, ... ] ] ) -> int i

math.bit_xor

 ( int a1 [, int a2 [, ... ] ] ) -> int i

math.bit_inv

 ( int a1 ) -> int i

math.bit_bits (result = result | (1 << a1) | (1 << a2) | ...;)

 ( int a1 [, int a2 [, ... ] ] ) -> int i


-- Note: there are no bit shift. Use those Lua functions instead for 24 bits bitshift
-- 24 bits because only the 24 bits of the mantissa can be easily used in a 32 bit float
-- bitshift functions (<<, >> equivalent)
-- shift left
local function lsh(value,shift)
    return (value*(2^shift)) % 2^24
end
-- shift right
local function rsh(value,shift)
    return math.floor(value/2^shift) % 2^24
end