diff options
author | Fedja Beader <fedja@protonmail.ch> | 2025-01-29 19:21:13 +0100 |
---|---|---|
committer | Jesusalva Jesusalva <jesusalva@tmw2.org> | 2025-05-03 14:50:16 +0000 |
commit | 9ea5916f54b6af525487f033e3df1813f6a46ddc (patch) | |
tree | d7ba769badcfc502e4bceb7cfb69bc50c5007efa | |
parent | ab7ee05b385c3da2e5437379f4511192fbc72618 (diff) | |
download | serverdata-9ea5916f54b6af525487f033e3df1813f6a46ddc.tar.gz serverdata-9ea5916f54b6af525487f033e3df1813f6a46ddc.tar.bz2 serverdata-9ea5916f54b6af525487f033e3df1813f6a46ddc.tar.xz serverdata-9ea5916f54b6af525487f033e3df1813f6a46ddc.zip |
Simplify bitmask_count
-rw-r--r-- | npc/functions/bitwise.txt | 24 |
1 files changed, 8 insertions, 16 deletions
diff --git a/npc/functions/bitwise.txt b/npc/functions/bitwise.txt index eadfb30ce..38021e715 100644 --- a/npc/functions/bitwise.txt +++ b/npc/functions/bitwise.txt @@ -47,25 +47,17 @@ function script bitwise_set { } // bitmask_count(<int>) -// returns the number of bits set in <int> (up to 4096?) +// returns the number of bits set in <int> 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) { - // Safeguard (2,147,483,647 is INT_MAX) - if (.@m >= 31) - break; - // Actual calculation - .@s=2**.@m; - if (.@n & .@s) - .@p++; - .@m++; + .@ones = 0; // Bits set + + while (.@n > 0) { + if (.@n & 1) + .@ones ++; + .@n >>= 1; } - return .@p; + return .@ones; } ///////////////////////////////////////////////////////////////////////////////// |