Optimize by replacing modulo with bitemask