lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


>> Has anybody tried to implement bitwise logical operators in Lua?
>> Like for flags etc?
>
> http://www.mupsych.org/~rrt/Lua

If you only need single-bit flags then the Lua code below could be used.
Functions "testflag" and "setflag" take twice as long as the "band" and
"bor" functions from RT's bitlib.  Function "clrflag" runs at the same speed
as its bitlib counterpart (that takes two calls: to bnot and band.)


do
local mod = math.mod

function testflag(set, flag)
    return mod(set, 2*flag) >= flag
end

function setflag(set, flag)
    if mod(set, 2*flag) >= flag then
        return set
    end
    return set + flag
end

function clrflag(set, flag)
    if mod(set, 2*flag) >= flag then
        return set - flag
    end
    return set
end
end


--
Wim