Coding · stretch-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix
Number of 1 Bits
easybit-manipulation
Problem
Given a non-negative integer n that fits in 32 bits, return how many bits are set to 1 in its binary representation (its Hamming weight).
0 <= n <= 2^32 - 1- The input is to be treated as an unsigned 32-bit value;
2^32 - 1has all 32 bits set.
Examples
n = 25→3, since25 = 0b11001.n = 64→1, since64 = 0b1000000.n = 2^32 - 1→32; every bit of the unsigned 32-bit value is set.
Hints
- You can inspect the lowest bit with
n & 1and discard it withn >> 1. What does looping over that give you? - Instead of visiting all 32 bit positions, can you jump directly from one set bit to the next?
n & (n - 1)clears the lowest set bit. Count how many times you can do that before reaching 0.
Last reviewed 8 September 2026
Locked
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Secure payment by Stripe · instant access · refund questions: [email protected]
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.