Bit Manipulation

Master bitwise operations and binary manipulation techniques.

Overview

Bit manipulation involves direct manipulation of bits in binary representation. This section covers bitwise operators and their clever applications in solving problems efficiently.

Key Concepts

  • Bitwise operators (AND, OR, XOR, NOT, shifts)
  • Bit masks and flags
  • Setting, clearing, and toggling bits
  • Counting set bits
  • Power of two checks
  • XOR properties

Common Problems

Easy

  • Single Number
  • Number of 1 Bits
  • Reverse Bits
  • Power of Two

Medium

  • Single Number II
  • Single Number III
  • Bitwise AND of Numbers Range
  • Maximum XOR of Two Numbers

Hard

  • Maximum XOR with an Element from Array
  • Minimum XOR Sum of Two Arrays

Practice Tips

  1. Know operators: Master AND, OR, XOR, NOT, left/right shifts
  2. XOR properties: a ^ a = 0, a ^ 0 = a, commutative
  3. Bit tricks: Learn common patterns (isolate rightmost bit, etc.)
  4. Binary representation: Understand two’s complement

Resources