---
# System prepended metadata

title: 191. Number of 1 Bits
tags: [mock interview, 面試]

---

# 191. Number of 1 Bits
# 233. Number of Digit One

{%hackmd theme-dark %}


> n = 00000000000000000000000000001011
> 8+2+1 = 11
> ans = 3
> 

```cpp=
int numberOf1(int n) {
    int cnt = 0;
    while (n != 0) {
        if (n % 10 == 1) {
            cnt++;
        }
        n /= 10;
    }
    return cnt;
}

```
000000001011 & 1 = 1, cnt = 1
000000000101 & 1 = 1, cnt = 2;
000000000010 & 1 = 0, cnt = 2;
000000000001 & 1 = 1, cnt = 3;


> n = 12381111323
> n = -1211






> Input: n = 13
> Output: 6
> 1 2 ... 10 11 12 13
> ^       ^  ^^ ^  ^

[1 ~ 9] : 1
[10 ~ 19]: 9 + [1~9] = 10
[20 ~ 29]: [1~9] = 1
[101~ 109]: 9 + [01~09] = 10

===

1. possible
2. ditgital by digital

> 23[1]47
> [0-22]1[XX] 23*100  00100 - 22199

> if >= 1
    x == 1
    x > 1 
> == 0 // 23047
> 



###### tags: `mock interview` `面試`