---
# System prepended metadata

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

---

# 191. Number of 1 Bits

{%hackmd theme-dark %}

   10001000 x
   10000111 x-1
 & 10000000
[191. Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/)
[233. Number of Digit One](https://leetcode.com/problems/number-of-digit-one/)

11100111

-123


```python=
// -123 -> 1                  (-3) -12 -> (-2) -1 -> (-1)
// -121 -> 2
// &1 = %2

// int : -2^31 ~ 2^31 -1
// long long : 最小值不可以轉正 -> 判斷是否 LongLongMIN
int countDigitOneOfX(long long x){
    if(xx<0)    xx = -xx;
    if(x>=0){
        int ans = 0;
        while(x){
            if(x % 10 == 1){
                ans ++;
            }
            x /= 10;
        }
    }else{
        int ans = 0;
        while(x){
            if(x % 10 == -1){ // ?????
                ans ++;
            }
            x /= 10;
        }
    }
}

```

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