Friday, November 21, 2025
HomeLanguagesPHP | gmp_popcount() Function

PHP | gmp_popcount() Function

The gmp_popcount() is a built-in function in PHP which is used to find the population count of a GMP number (GNU Multiple Precision : For large numbers). We can also say that this function is used to find the number of set bits in the binary representation of a GMP number.

Syntax:

gmp_popcount ( $num )

Parameters: This function accepts a GMP number $num as a mandatory parameter as shown in the above syntax. This parameter can be a GMP object in PHP version 5.6 and later, or we are also allowed to pass a numeric string provided that it is possible to convert that string to a number.

Return Value: This function returns an integer which is the population count or the number of set bits in binary representation of a GMP number passed to it as parameter.

Examples:

Input : "9"
Output : 2

Input : "25"
Output : 3

Below programs illustrate the gmp_popcount() function in PHP :

Program 1: Program to calculate the population count of a number when numeric strings as GMP numbers are passed as arguments.




<?php
// PHP program to calculate population count 
// of a GMP number passed as arguments 
  
// strings as GMP numbers 
$num1 = "9";
$num2 = "25";
  
// calculates the population count of a number
$pcount = gmp_popcount($num1);
echo $pcount."\n";
  
// calculates the population count of a number
$pcount = gmp_popcount($num2);
echo $pcount."\n";
  
?>


Output:

2
3

Program 2: Program to calculate the population count of a number when GMP numbers are passed as arguments.




<?php
// PHP program to calculate population count 
// of a GMP number passed as arguments 
  
// creating GMP numbers using gmp_init()
$num1 = gmp_init(9, 10);
$num2 = gmp_init(25, 10);
  
// calculates the population count of a number
$pcount = gmp_popcount($num1);
echo $pcount."\n";
  
// calculates the population count of a number
$pcount = gmp_popcount($num2);
echo $pcount."\n";
  
?>


Output:

2
3

Reference:
http://php.net/manual/en/function.gmp-popcount.php

Last Updated :
14 Apr, 2018
Like Article
Save Article

<!–

–>

Similar Reads
Related Tutorials
RELATED ARTICLES

Most Popular

Dominic
32405 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6781 POSTS0 COMMENTS
Nicole Veronica
11928 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11997 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7166 POSTS0 COMMENTS
Thapelo Manthata
6862 POSTS0 COMMENTS
Umr Jansen
6847 POSTS0 COMMENTS