gmp_popcount


gmp apg

GET the population count of 1 in a given GMP number as an INTEGER, considering this number in base 2.





This function returns population count of $num as an INTEGER.



<?php

int gmp_popcount 
( GMP|int|string $num )

where,

$num = The GMP number to count the population

?>
 

$num


The GMP number to population count.



  1 EXERCISE   

<?php

// All values are considered in base 2, 
// therefore, converted to this base

$arr01 = [ 2021, '011111100101', '03745', '0x7e5'];

foreach(
$arr01 as $a)
{
    
$popc01 = gmp_popcount($a);
    
    echo 
'gmp_popcount( ' . $a . ' ) = ' . $popc01 . '<br><br>';
    
}

?> 

  2 EXERCISE   

<?php

// All values are considered in base 2, 
// therefore, converted to this base

echo "gmp_popcount(-1) = " . 
                   
gmp_popcount(-1) . '<br><br>';

echo 
"gmp_popcount(0) = " . 
                   
gmp_popcount(0) . '<br><br>';

echo 
"gmp_popcount(12123) = " . 
                   
gmp_popcount(12123) . '<br><br>';

echo 
"gmp_popcount(\"52638927634234\") = " . 
                   
gmp_popcount("52638927634234") . '<br><br>';

echo 
"gmp_popcount(\"-23476123423433\") = " . 
                   
gmp_popcount("-23476123423433") . '<br><br>';

echo 
"gmp_popcount(PHP_INT_MAX) = " . 
                   
gmp_popcount(PHP_INT_MAX) . '<br><br>';

$n = gmp_init("9876546789222");
echo 
"gmp_popcount($n) = " . 
                   
gmp_popcount($n) . '<br><br>';

try {
    
var_dump(gmp_popcount(array()));
} catch (
\TypeError $e) {
    echo 
$e->getMessage() . "<br><br>";
}

?>