Module:CommonData/Bit

-- -- Module that provides an interface with Lua bit --

local p = {}

function p.bit_band(a, b)   local result = 0 local bitval = 1 while a > 0 and b > 0 do     if a % 2 == 1 and b % 2 == 1 then result = result + bitval end bitval = bitval * 2 a = math.floor(a/2) b = math.floor(b/2) end return result end

--local bor, bxor, band = 1, 3, 4 local function _bit(oper, a, b)  local result, bitval, s = 0, 2147483648 --2^31 repeat s,a,b = a + b + bitval, a % bitval, b % bitval result = result + bitval * oper % (s-a-b) bitval = bitval / 2 until bitval < 1 return result end

--- Check if value contains all flags using bitwise and -- -- @return empty if not all bits match, otherwise flags function p.Isflag( value, flags ) value,flags = tonumber(value) or 0, tonumber(flags) or 0 return _bit(4, value, flags) == flags and tostring(flags) or "" end

return p