PyDocs

Nav

Python Runtime Services

int.bit_count

builtins

method of int · Added in 3.10
  • int.bit_count()
Number of ones in the binary representation of the absolute value of self.

Also known as the population count.

>>> bin(13)
'0b1101'
>>> (13).bit_count()
3
1 Example
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
from the Python docs
See Also
No see-alsos for builtins/int.bit_count
0 Notes
No notes for int.bit_count