# 12. 191. Number of 1 Bits

{% embed url="<https://leetcode.com/problems/number-of-1-bits/description/>" %}

bitwise operators

[JavaScript Bitwise Operators (with Examples)](https://www.programiz.com/javascript/bitwise-operators)

#### with bitwise

```javascript
/**
 * @param {number} n - a positive integer
 * @return {number}
 */
var hammingWeight = function (n) {
    let cnt = 0;
    while (n ^ 0) {
        cnt = cnt + (n & 1);
        n = n >>> 1;
    }
    return cnt;
};


```

#### without bitwise

```javascript
/**
 * @param {number} n - a positive integer
 * @return {number}
 */
const memo = {};
var hammingWeight = function(n) {
    if (memo[n] !== undefined) return memo[n];
    const bi = n.toString(2);
    let cnt = 0;
    for (let i = 0; i < bi.length; i++) {
        if (bi.charAt(i) === '1') cnt++;
    }
    memo[n] = cnt;
    return cnt
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://algorithm.prettylog.com/top-75-leetcode-questions-to-save-your-time/problems/binary/12.-191.-number-of-1-bits.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
