Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php

What is gmp_popcount() in PHP?

Abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The gmp_popcount() function in PHP is used to find the population count in the given number. The number of set bits in the number is called the population count of the number.

Syntax

gmp_popcount(GMP|int|string $num): int

Parameters

  • num: It is the GMP object, integer, or a string whose population count needs to be calculated.

Return value

The method returns an integer denoting the population count of num.

Code example

Let’s look at the code below:

<?php
$count = gmp_popcount(11);
print "Number of set bits in 11 is ".$count."\n";
$count = gmp_popcount("15");
print "Number of set bits in 15 is ".$count."\n";
?>
gmp_popcount() method

Code explanation

  • Line 2: We invoke the gmp_popcount() method to obtain the number of set bits in 11.
  • Line 3: We print the population count of 11.
  • Line 5: We invoke the gmp_popcount() method to obtain the number of set bits in 15.
  • Line 6: We print the population count of 15.

RELATED TAGS

php

CONTRIBUTOR

Abhilash
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring