note 126609 added to function.gmp-popcount
| From: | ketrab2004 at main dot php dot net | Date: | Sun, 14 Nov 2021 14:18:33 +0000 |
| Subject: | note 126609 added to function.gmp-popcount | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-217246@lists.php.net to get a copy of this message | ||
Another way to get the population count when you don't have the gmp extension is using bitwise
operations:
<?php
$int = 133; // 10000101
for($count = 0; $int != 0; $count++) // repeat until $int is 0 (and count the amount of steps it
takes in $count)
{
$int = $int & $int-1; // remove the right most 1 from $int using the bitwise and operator
}
echo $count; // 3
?>
This is Kernighan's population count.
https://youtu.be/ZRNO-ewsNcQ?t=510 has a nice
explanation on how it works
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 2a02:a465:b370:1:993a:57ec:2bc5:7b3d)
----
Manual Page -- https://php.net/manual/en/function.gmp-popcount.php
Edit -- https://main.php.net/note/edit/126609
Del: integrated -- https://main.php.net/note/delete/126609/integrated
Del: useless -- https://main.php.net/note/delete/126609/useless
Del: bad code -- https://main.php.net/note/delete/126609/bad+code
Del: spam -- https://main.php.net/note/delete/126609/spam
Del: non-english -- https://main.php.net/note/delete/126609/non-english
Del: in docs -- https://main.php.net/note/delete/126609/in+docs
Del: other reasons-- https://main.php.net/note/delete/126609
Reject -- https://main.php.net/note/reject/126609
Search -- https://main.php.net/manage/user-notes.php