Skip to main content

Documentation Index

Fetch the complete documentation index at: https://base.bangwu.me/llms.txt

Use this file to discover all available pages before exploring further.

Bit manipulation

Bitwise operations work directly on binary bits. They are efficient and commonly used for algorithm optimization.

Basic bit operators

OperatorsNameDescriptionExample
&Bitwise ANDThe result is 1 only when both bits are 15 & 3 = 1 (101 & 011 = 001)
``Bitwise ORThe result is 1 when either bit is 1`53 = 7` (101011 = 111)
^Bitwise XORThe result is 1 when the bits are different5 ^ 3 = 6 (101 ^ 011 = 110)
~Bitwise NOTFlip 0 to 1 and 1 to 0~5 = -6
<<Left shiftShift left by n bits, equivalent to multiplying by 2^n5 << 1 = 10
>>Right shiftShift right by n bits, roughly equivalent to dividing by 2^n5 >> 1 = 2

Common tricks

1. Check odd or even

2. Swap two numbers without a temporary variable

3. Check whether the k-th bit is 1

4. Set the k-th bit to 1

5. Set the k-th bit to 0

6. Flip the k-th bit

7. Extract the lowest 1 bit

8. Remove the lowest 1 bit

9. Check whether a number is a power of two

10. Count the number of 1 bits in binary

Classic problems

LeetCode 136. Single Number

Problem: every number in the array appears twice except one. Find the one that appears only once. Idea: use the XOR properties a ^ a = 0 and a ^ 0 = a.
class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        ans = 0
        for num in nums:
            ans ^= num
        return ans

LeetCode 191. Number of 1 Bits

class Solution:
    def hammingWeight(self, n: int) -> int:
        count = 0
        while n:
            n &= n - 1
            count += 1
        return count

LeetCode 231. Power of Two

class Solution:
    def isPowerOfTwo(self, n: int) -> bool:
        return n > 0 and (n & (n - 1)) == 0

LeetCode 338. Counting Bits

class Solution:
    def countBits(self, n: int) -> List[int]:
        dp = [0] * (n + 1)
        for i in range(1, n + 1):
            dp[i] = dp[i >> 1] + (i & 1)
        return dp

Typical use cases

  • State compression: represent multiple boolean states with one integer
  • Set operations: implement intersection, union, and complement through bit masks
  • Permission systems: store different permissions as bits
  • Performance optimization: use fast bitwise operations to replace some multiply/divide cases
Last modified on April 17, 2026