[ Arithmetic | Reference Manual | Alphabetic Index ]

popcount(+Number, -Result)

-Result is popcount(+Number)

Result is the number of 1s in Number's binary representation
Number
Positive Integer
Result
Output: integer.

Description

Result is the number of nonzero bits in Number's binary representation. Only defined for non-negative Numbers.

This predicate can be used as a function in arithmetic expressions. In coroutining mode, if Number is uninstantiated, the call is delayed until it is instantiated.

Modes and Determinism

Exceptions

(4) instantiation fault
Number or Index is not instantiated (non-coroutining mode only).
(5) type error
Number or Index is a number but not an integer
(6) out of range
Number is negative
(24) number expected
Number or Index is not of a numeric type

Examples

    Result is popcount(0).           % gives Result = 0.
    Result is popcount(1).           % gives Result = 1.
    Result is popcount(10).          % gives Result = 2. (10 is binary 1010)
    Result is popcount(31).          % gives Result = 5. (31 is binary 11111)
    Result is popcount(32)           % gives Result = 1. (32 is binary 100000)

See Also

is / 2, getbit / 3, clrbit / 3, setbit / 3, lsb / 2, msb / 2