# 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
};
```
