Bits
PopCount
compute number of bits that are 1 in an integer
Calling Sequence
Parameters
Description
Examples
Compatibility
PopCount(num)
num
-
an integer
The PopCount command computes the number of bits in num that are equal to 1.
If num is negative, infinitely many bits are equal to 1, so the returned value is infinity.
This operation is often called popcount in other systems. The name stands for "population count".
with⁡Bits:
Construct an integer consisting of the following bits.
bits≔1,1,0,0,0,1,0,1,0,1,1,0,1,1,1
num≔Join⁡bits
num≔30371
There are 9 bits in num that are equal to 1.
PopCount⁡num
9
We can see this by adding the entries of bits, too.
add⁡bits
In fact, PopCount(n) = add(Split(n)) for all positive integers n. It is more efficient to compute PopCount(n) than add(Split(n)).
The Bits[PopCount] command was introduced in Maple 2025.
For more information on Maple 2025 changes, see Updates in Maple 2025.
See Also
Join
Split
Download Help Document