Bit Manipulation
Bitwise AND, OR, XOR, NOT, and shifts. Checking, setting, clearing, and toggling bits. The foundation for bitmask techniques.
Lessons
1. Intro
What bit manipulation is
2. What Are Bits?
Binary foundation
3. Binary Representation
Reading binary numbers
4. Bitwise AND (&)
Both bits must be 1
5. Bitwise OR (|)
At least one bit is 1
6. Bitwise XOR (^)
Bits differ
7. Bitwise NOT (~)
Flip all bits
8. Left Shift (<<)
Multiply by powers of 2
9. Right Shift (>>)
Divide by powers of 2
10. Quiz: Basic Operators
Knowledge check
11. Checking if a Bit is Set
Test individual bits
12. Setting a Bit
Turn a bit on
13. Clearing a Bit
Turn a bit off
14. Toggling a Bit
Flip a bit
15. Common Operations Summary
Quick reference
16. Problem - Number of 1 Bits
LeetCode 191
17. Count Bits - Checking Each Position
Loop through all bits
18. Count Bits - Brian Kernighan's Algorithm
Clever bit trick
19. Count Bits - Implementation
The code
20. Quiz: Counting Bits
Knowledge check
21. Checking Power of Two
One-line trick
22. Problem - Power of Two
LeetCode 231
23. Power of Two - Implementation
The code
24. Isolating the Rightmost Set Bit
Another useful trick
25. XOR Properties
Magic of XOR
26. Problem - Single Number
LeetCode 136
27. Single Number - The Idea
XOR cancellation
28. Single Number - Implementation
The code
29. Quiz: XOR
Knowledge check
30. Bitmasks for Subsets
Representing sets as bits
31. Iterating Through Subsets
Generate all subsets
32. Subset Operations
Union, intersection, difference
33. Problem - Subsets
LeetCode 78
34. Subsets - Bitmask Approach
Using bit manipulation
35. Subsets - Implementation
The code
36. Quiz: Bitmasks
Knowledge check
37. Swapping Without a Temporary
XOR swap trick
38. Reversing Bits
Mirror the bits
39. Bit Manipulation Tricks Summary
Quick reference
40. When to Use Bit Manipulation
Recognizing the pattern
41. Section Recap
What we learned