> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Number of 1 Bits Python Solution with Tests

> Tested Python solution for LeetCode 191 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 191, Easy. Topics: Divide and Conquer, Bit Manipulation. [View on LeetCode](https://leetcode.com/problems/number-of-1-bits/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 191   # by problem number
lcpy gen -s number_of_1_bits   # by problem name
```

## 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

```
Input: n = 11
Output: 3
Explanation:
The input binary string 1011 has a total of three set bits.
```

```
Input: n = 128
Output: 1
Explanation:
The input binary string 10000000 has a total of one set bit.
```

```
Input: n = 2147483645
Output: 30
Explanation:
The input binary string 1111111111111111111111111111101 has a total of thirty set bits.
```

### Constraints

* 1 \<= n \<= 2^31 - 1

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_1_bits/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_1_bits/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(1) - at most 32 iterations (number of bits in int)
    # Space: O(1) - only using constant extra space
    def hamming_weight(self, n: int) -> int:
        """
        Count the number of set bits (1s) in the binary representation of n.

        Uses the Brian Kernighan's algorithm:
        n & (n-1) removes the rightmost set bit from n.
        We keep doing this until n becomes 0, counting each iteration.

        This is more efficient than checking each bit individually
        because it only iterates for the number of set bits, not all bits.
        """
        count = 0
        while n:
            count += 1
            n &= n - 1  # Remove the rightmost set bit
        return count
```

## Complexity

| Time                                                 | Space                                  |
| ---------------------------------------------------- | -------------------------------------- |
| O(1) - at most 32 iterations (number of bits in int) | O(1) - only using constant extra space |

## Tags

[Grind](/catalog/grind), [Blind 75](/catalog/blind-75), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
