From e399392302d088576fb0a830b951b085760deb9e Mon Sep 17 00:00:00 2001 From: Jesusaves Date: Thu, 5 Sep 2019 11:59:54 -0300 Subject: New math function: bitmask_count() - counts how many bits are set --- npc/functions/math.txt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'npc/functions') diff --git a/npc/functions/math.txt b/npc/functions/math.txt index fc27bec40..9c93fbb74 100644 --- a/npc/functions/math.txt +++ b/npc/functions/math.txt @@ -102,3 +102,22 @@ function script ponderate_avg { return (.@h1+.@h2)/.@dd; } +// bitmask_count() +// returns the number of bits set in (max. 4096) + +function script bitmask_count { + .@n = getarg(0); // Number evaluated + .@p=0; // Bits set/unset + .@s=0; // Stack and Check + .@m=0; // Memory + + // Loop only as needed + while (.@s < .@n) { + .@s=2**.@m; + if (.@n & .@s) + .@p++; + .@m++; + } + return .@p; +} + -- cgit v1.2.3-60-g2f50