Integer Methods

int.bit_count()

Count the set bits of the magnitude of an integer (population count). Negative integers are treated by absolute value, not by counting infinitely extended sign bits. Zero has no set bits. Useful for counting enabled flags in a nonnegative bit mask.

Formal Syntax & Structure

python syntaxpython
value.bit_count()

Return Value

int

Number of one bits in abs(value).

Working Example

Example Snippetpython
flags = 0b101101print(flags.bit_count())print((-flags).bit_count())print((0).bit_count())
Python Reference