Voting

: nine plus zero?
(Example: nine)

The Note You're Voting On

ketrab2004
3 years ago
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

<< Back to user notes page

To Top