note 123355 added to function.gmp-popcount

From: Date: Sun, 18 Nov 2018 15:51:54 +0000
Subject: note 123355 added to function.gmp-popcount
Groups: php.notes 
Request: Send a blank email to php-notes+get-209723@lists.php.net to get a copy of this message
If you don't have gmp extension enabled (or don't want to use it for any reason), you can get popcount of an int using decbin() and substr_count(). <?php $int1 = 133; $bin1 = decbin($int1); // "10000101" echo substr_count($bin1, "1"); // Result: 3 ?> Being a string-comparison this is far less efficient than gmp_popcount() (for which there is a dedicated instruction on most if not all modern processors), but may be handy if gmp is unavailable, or in non-performance-critical code that doesn't otherwise need it. ---- Server IP: 208.43.231.9 Probable Submitter: 62.49.1.208 ---- Manual Page -- http://php.net/manual/en/function.gmp-popcount.php Edit -- https://master.php.net/note/edit/123355 Del: integrated -- https://master.php.net/note/delete/123355/integrated Del: useless -- https://master.php.net/note/delete/123355/useless Del: bad code -- https://master.php.net/note/delete/123355/bad+code Del: spam -- https://master.php.net/note/delete/123355/spam Del: non-english -- https://master.php.net/note/delete/123355/non-english Del: in docs -- https://master.php.net/note/delete/123355/in+docs Del: other reasons-- https://master.php.net/note/delete/123355 Reject -- https://master.php.net/note/reject/123355 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#209723) next »