ID EN
Built-in Types

bit_count

Python 3.11

Return the number of ones in the binary representation of the absolute value of the integer. This is also known as the population count. Example:

Syntax

PYTHON
int.bit_count()

Examples

Example 1
PYTHON
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
Example 2
PYTHON
def bit_count(self):
    return bin(self).count("1")