diff options
author | Fedja Beader <fedja@protonmail.ch> | 2025-01-29 19:21:13 +0100 |
---|---|---|
committer | Fedja Beader <fedja@protonmail.ch> | 2025-02-12 21:07:35 +0000 |
commit | 09a8c3b617aabcd871d7980170d7336a229fdcdb (patch) | |
tree | 57d98d9832d76fc693e34ce114d588d01b1f2d17 | |
parent | 11a59ad44a3098f7d4a5f527f5e370f6de5998cd (diff) | |
download | serverdata-09a8c3b617aabcd871d7980170d7336a229fdcdb.tar.gz serverdata-09a8c3b617aabcd871d7980170d7336a229fdcdb.tar.bz2 serverdata-09a8c3b617aabcd871d7980170d7336a229fdcdb.tar.xz serverdata-09a8c3b617aabcd871d7980170d7336a229fdcdb.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; } ///////////////////////////////////////////////////////////////////////////////// |