Module:Bit

From Binding of Isaac: Rebirth Wiki
Jump to: navigation, search

Documentation for this module may be created at Module:Bit/doc

local p = {}

local bit32 = require( 'bit32' )

local w_frame = require( 'module:frame' )
local w_table = require( 'module:table' )


p['and'] = function ( f )
	return bit32.band( unpack( w_table.map( w_frame.args( f ), tonumber ) ) )
end


function p.count( f )
    local dlc    = tonumber( w_frame.args( f )[1] ) or 0
    local result = 0
    while dlc ~= 0 do
        result = result + bit32.band( dlc, 1 )
        dlc    = bit32.rshift( dlc, 1 )
    end
    return result
end


function p.mask( f )
	local args   = w_frame.args( f )
	local result = tonumber( args[1] ) or 0
	for i = 2, #args do
		result = bit32.band( result, bit32.bnot( tonumber( args[i] ) ) )
	end
	return result
end


p['or'] = function ( f )
	return bit32.bor( unpack( w_table.map( w_frame.args( f ), tonumber ) ) )
end


return p