PHP - gmp_popcount() Function



Definition and Usage

The gmp_popcount() function calculates the population count.

Description

The gmp_popcount() counts the number of non-zero entries ('1' bits) in given byte, string etc.

Syntax

 gmp_popcount ( GMP $a ) : int 

Parameters

Sr.No Parameter & Description
1

a

It can a GMP resource number , a gmp object or a numeric string.

Return Values

PHP gmp_popcount() function returns population count as an integer.

PHP Version

This function will work from PHP Version greater than 5.0.0.

Example 1

Working of gmp_popcount −

 <?php $pc = gmp_init("10000101", 2); $num = gmp_popcount($pc); echo "The result is : ".$num; ?> 

This will produce following result −

 The result is : 3 

Example 2

Working of gmp_popcount() −

 <?php $pc = gmp_init("1011111101", 2); $num = gmp_popcount($pc); echo "The result is : ".$num; ?> 

This will produce following result −

 The result is : 8 
php_function_reference.htm
Advertisements