---
# System prepended metadata

title: \#191 Number of 1 Bits
tags: [leetcode, leetcode-easy]

---

# \#191 Number of 1 Bits
## *傳入一正整數,計算其二進位出現多少次"1"*
## Log
 - build 20210421 by syhuang

## xxx
 - xxx
```javascript=
```
## 初見2
 - 正常解法:bit運算搭配&1跑迴圈
```javascript=
var hammingWeight = function(n) {
    var result=0;
    while(n){
        if(parseInt(n&1)) result++;
        n>>>=1;
    }
    return result;
};
```
## 初見1
 - 轉2進位字串陣列後, 迴圈逐個計算"1"出現的次數
```javascript=
var hammingWeight = function(n) {
    var binaryAry = n.toString(2).split('');
    var result=0;
    for(var i=0; i<binaryAry.length; i++){
        if(parseInt(binaryAry[i])) result++;
    }
    return result;
};
```
## 備註
 -unsigned binary shift use ">>>" operator
## 參考
###### tags: `leetcode`, `leetcode-easy`