LeetCode 191, Easy. Topics: Divide and Conquer, Bit Manipulation. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 16 parametrized pytest cases, and a playground notebook:
Problem
Given a positive integer n, write a function that returns the number of set bits in its binary representation (also known as the Hamming weight).
Examples
Constraints
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
Grind, Blind 75, NeetCode 150, NeetCode 250, NeetCode All. Last modified on September 7, 2026