int.bit_count()

Population count: how many bits are ON. Like bit_length, it works on the magnitude, so n and -n give the same answer.

Int methodPython 3.10+Live demo
Common call
n.bit_count()
Returns
int — the number of 1 bits
Replaces
bin(n).count("1")
Watch out
Python 3.10+ only — there is no builtin equivalent before that
int.bit_count()
→ int

Demo

Live evaluation
Try:
Inputs
nintinteger to count bits in
Output
(0).bit_count()
0

bit_count counts the 1s in the binary form. 7 is 111, so three. 255 is 11111111, so eight. Any power of two has exactly one bit set. The sign is dropped before counting, which is why -7 also gives three — this counts the magnitude, not a two-complement representation.

Common patterns

Hamming distance between two ints
XOR marks the differing bits; bit_count tallies them.
distance = (a ^ b).bit_count()
Test for a power of two
Exactly one bit set means a power of two.
is_power_of_two = n > 0 and n.bit_count() == 1
Count members of a bitmask set
When flags are packed into an int, the set size is the popcount.
enabled_flags = mask.bit_count()

Examples

1. Zero has none
(0).bit_count()
Returns
0
2. Three set bits
(7).bit_count()
Returns
3
3. Full byte
(255).bit_count()
Returns
8
4. Power of two
(64).bit_count()
Returns
1
5. Sign is ignored
(-7).bit_count()
Returns
3
6. Hamming distance
(0b1011 ^ 0b1101).bit_count()
Returns
2

Pitfalls

1. Python 3.10 or newer only
Calling it on an older interpreter raises AttributeError. If you support older versions you need a fallback.
Breaks on 3.9
(7).bit_count()
AttributeError: 'int' object has no attribute 'bit_count'
Portable fallback
try:
    count = n.bit_count()
except AttributeError:
    count = bin(abs(n)).count("1")
works everywhere
2. Negatives count the magnitude, not two-complement
A real two-complement -7 in 8 bits is 11111001, which has six set bits. Python counts abs(-7) = 7 instead, giving three. Mask first if you want machine semantics.
Not machine bits
(-7).bit_count()
3
Mask to a width
(-7 & 0xFF).bit_count()
6 # 11111001
3. Confused with bit_length
bit_length is how WIDE the number is; bit_count is how many bits are ON. They agree only when every bit up to the top is set, like 1, 3, 7, 15.
Different questions
(8).bit_length(), (8).bit_count()
(4, 1)
Pick deliberately
width = n.bit_length()
ones  = n.bit_count()
two distinct facts

When to use

Use it
  • Hamming distance and similarity over bit vectors
  • Counting set flags in a packed bitmask
  • Power-of-two checks that read clearly
  • Any hot loop where bin(n).count("1") was the old workaround
Reach for something else
  • Codebases that must run on Python 3.9 or older
  • Wanting the bit WIDTH → bit_length
  • Two-complement bit counting → mask to a fixed width first

Notes

Complexity
O(number of digits) — a per-digit popcount, no Python-level loop
Return
A non-negative int; 0 only for the input 0
CPython impl
Objects/longobject.c :: long_bit_count
Memory
No allocation — unlike the bin(n).count("1") workaround, which builds a string
Thread-safe
Yes — ints are immutable

FAQ

Population count — the number of 1 bits in a value. It is a single CPU instruction on modern hardware, and bit_count is Python exposing that idea. In information theory the same quantity applied to a XOR is the Hamming weight.

(0b1011).bit_count()
# 3

History

3.10
int.bit_count added, exposing a popcount without the bin(...).count("1") workaround.