diff options
Diffstat (limited to 'npc')
-rw-r--r-- | npc/functions/math.txt | 19 |
1 files changed, 19 insertions, 0 deletions
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(<int>) +// returns the number of bits set in <int> (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; +} + |