summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesusaves <cpntb1@ymail.com>2019-09-05 11:59:54 -0300
committerJesusaves <cpntb1@ymail.com>2019-09-05 11:59:54 -0300
commite399392302d088576fb0a830b951b085760deb9e (patch)
tree27861860090b7d66d73265971e517c4f9bbf2b75
parent48effc93a8267b01286b937b9bea067d1634e35f (diff)
downloadserverdata-e399392302d088576fb0a830b951b085760deb9e.tar.gz
serverdata-e399392302d088576fb0a830b951b085760deb9e.tar.bz2
serverdata-e399392302d088576fb0a830b951b085760deb9e.tar.xz
serverdata-e399392302d088576fb0a830b951b085760deb9e.zip
New math function: bitmask_count() - counts how many bits are set
-rw-r--r--npc/functions/math.txt19
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;
+}
+