Lua BitOps: Difference between revisions

From Spring
Jump to navigationJump to search
Koshi (talk | contribs)
fix category
→Bit Operators: move to luacallout format
 
(2 intermediate revisions by 2 users not shown)
Line 2: Line 2:


== Bit Operators ==
== Bit Operators ==
'''math.bit_and'''
  ( int a1 [, int a2 [, ... ] ] ) -> int i


'''math.bit_or'''
{{LuaCallout
  ( int a1 [, int a2 [, ... ] ] ) -> int i
|prefix = math.
|name = bit_and
|arg1 =  {{type|number}} a1
|arg2 =  {{type|number}} a2 , ...
|arg3 = 
|arg4 =
|arg5 =
|arg6 =
|arg7 =
|arg8 =
|arg9 =
|return = {{type|number}} i
|info = Returns the bitwise AND of all arguments. Only use up to 24 bit integers.
}}


'''math.bit_xor'''
{{LuaCallout
  ( int a1 [, int a2 [, ... ] ] ) -> int i
|prefix = math.
|name = bit_or
|arg1 =  {{type|number}} a1
|arg2 =  {{type|number}} a2 , ...  
|arg3 = 
|arg4 =
|arg5 =
|arg6 =
|arg7 =
|arg8 =
|arg9 =
|return = {{type|number}} i
|info = Returns the bitwise OR of all arguments. Only use up to 24 bit integers.
}}


'''math.bit_inv'''
{{LuaCallout
  ( int a1 ) -> int i
|prefix = math.
|name = bit_xor
|arg1 =  {{type|number}} a1
|arg2 =  {{type|number}} a2 , ...
|arg3 =
|arg4 =
|arg5 =
|arg6 =
|arg7 =
|arg8 =
|arg9 =
|return = {{type|number}} i
|info = Returns the bitwise XOR of all arguments. Only use up to 24 bit integers.
}}


'''math.bit_bits''' <font color=#55ee99> (result = result | (1 << aX);)</font>
{{LuaCallout
  ( int a1 [, int a2 [, ... ] ] ) -> int i
|prefix = math.
|name = bit_inv
|arg1 =  {{type|number}} a1
|arg2 =
|arg3 =
|arg4 =
|arg5 =
|arg6 =
|arg7 =
|arg8 =
|arg9 =  
|return = {{type|number}} i
|info = Returns the bitwise NOT of the 24 bit integer argument.  
}}


{{LuaCallout
|prefix = math.
|name = bit_bits
|arg1 =  {{type|number}} a1
|arg2 =  {{type|number}} a2 , ...
|arg3 =
|arg4 =
|arg5 =
|arg6 =
|arg7 =
|arg8 =
|arg9 =
|return = {{type|number}} i
|info = Set each of the bits of a 24 bit integer. Returns result = result OR (1 << a1) OR (1 << a2) OR ...;)
}}
-- 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]]

Latest revision as of 12:28, 1 August 2022

Development < Lua Scripting < Lua BitOps

Bit Operators

math.bit_and ( number a1, number a2 , ... )
return: number i

Returns the bitwise AND of all arguments. Only use up to 24 bit integers.

math.bit_or ( number a1, number a2 , ... )
return: number i

Returns the bitwise OR of all arguments. Only use up to 24 bit integers.

math.bit_xor ( number a1, number a2 , ... )
return: number i

Returns the bitwise XOR of all arguments. Only use up to 24 bit integers.

math.bit_inv ( number a1 )
return: number i

Returns the bitwise NOT of the 24 bit integer argument.

math.bit_bits ( number a1, number a2 , ... )
return: number i

Set each of the bits of a 24 bit integer. Returns result = result OR (1 << a1) OR (1 << a2) OR ...;)
-- 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