# xuelangZF/LeetCode

Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
 .. Failed to load latest commit information. 136_SingleNumber.cpp 136_SingleNumber.py 137_SingleNumberII.cpp 137_SingleNumberII.py 169_MajorityElement.cpp 169_MajorityElement.py 190_ReverseBits.py 191_NumberOf1Bits.py 201_BitwiseANDofNumbersRange.py 231_PowerOfTwo.py 260_SingleNumberIII.cpp 260_SingleNumberIII.py 268_MissingNumber.py 29_DivideTwoIntegers.cpp 29_DivideTwoIntegers.py 318_MaximumProductOfWordLengths.py 338_CountingBits.cpp 338_CountingBits.py 342_PowerOfFour.cpp 342_PowerOfFour.py 371_SumOfTwoIntegers.cpp 371_SumOfTwoIntegers.py 78_Subsets.py README.md

# 常用位操作

• 判断奇偶数：num&0x1
• 丢弃最低位的1：num&(num-1)
• 对一个数变换符号：~num+1
• 对一个数取模(2^N )：num&0b11..1 （N个1）
• 返回 x，y中的最小值：y^((x^y) & -(x < y))

# 例子：更好的理解

## 260 Single Number III

``````2^k = (m^n) & (-m^n)
``````

## 338 Counting Bits

``````while(x){
count += 1;
x = x&(x-1); // 将最低位的1置为0
}
``````

``````ans[i] = ans[i >> 1] + (i&0x1)
# ans[i] = ans[i & (i - 1)] + 1
``````

``````while((x+1))
{
count++;
x=x|(x+1);
}
``````