#
##
###
a=0^a=a^0
0=a^a
a=a^b^b
###
a=a^b
b=a^b
a=a^b
### 1
a=n&(n-1)
### 1
diff=(n&(n-1))^n
##
[single-number](https://leetcode-cn.com/problems/single-number/)
> ****
```c++
int singleNumber(vector& nums) {
// 10 ^10 == 00
// 0
auto result = 0;
for (const auto &num : nums) {
result ^= num;
}
return result;
}
```
[single-number-ii](https://leetcode-cn.com/problems/single-number-ii/)
> ****
```c++
// 13
// 0
int singleNumber(vector& nums) {
auto ret = 0;
for (int i = 0; i < 32; ++i) {
auto sumOne = 0;
for (const auto &item : nums) {
sumOne += (item >> i) & 1;
}
ret ^= (sumOne % 3) `nums`
```c++
vector singleNumber(vector& nums) {
auto diff = 0;
//
for (const auto &item : nums) {
diff ^= item;
}
vector result(2);
// 1
// '1'
diff &= (-diff);
// 10
for (const auto &num : nums) {
if ((diff & num) == 0) {
result[0] ^= num;
} else {
result[1] ^= num;
}
}
return result;
}
```
> x & (-x)
>
> x -x '1' x '1'
>
> 1
> 10-108
> 10: 0000 1010
> -10: 1111 1010
> x & (-x) x'1'
[number-of-1-bits](https://leetcode-cn.com/problems/number-of-1-bits/)
> 1 [](https://baike.baidu.com/item/%E6%B1%89%E6%98%8E%E9%87%8D%E9%87%8F)
```c++
int hammingWeight(int n) {
auto ret = 0;
while (n != 0) {
n = n & (n - 1);
++ret;
}
return ret;
}
```
[counting-bits](https://leetcode-cn.com/problems/counting-bits/)
> **num** 0 i num i 1
```c++
vector countBits(int num) {
vector ret(num+1);
for (int i = 0; i = 1;
--pow;
}
return ret;
}
```
[bitwise-and-of-numbers-range](https://leetcode-cn.com/problems/bitwise-and-of-numbers-range/)
> [m, n] 0 >= 1;
++zeroNum;
}
return m