Bit Manipulation

Bitwise AND, OR, XOR, NOT, and shifts. Checking, setting, clearing, and toggling bits. The foundation for bitmask techniques.

41 lessons
151 min
Codeforces: 1000-1500LeetCode: 1300-1700

Lessons

1. Intro

What bit manipulation is

2m

2. What Are Bits?

Binary foundation

4m

3. Binary Representation

Reading binary numbers

5m

4. Bitwise AND (&)

Both bits must be 1

4m

5. Bitwise OR (|)

At least one bit is 1

4m

6. Bitwise XOR (^)

Bits differ

4m

7. Bitwise NOT (~)

Flip all bits

4m

8. Left Shift (<<)

Multiply by powers of 2

4m

9. Right Shift (>>)

Divide by powers of 2

5m

10. Quiz: Basic Operators

Knowledge check

2m1 problems

11. Checking if a Bit is Set

Test individual bits

4m

12. Setting a Bit

Turn a bit on

4m

13. Clearing a Bit

Turn a bit off

4m

14. Toggling a Bit

Flip a bit

4m

15. Common Operations Summary

Quick reference

4m

16. Problem - Number of 1 Bits

LeetCode 191

2m1 problems

17. Count Bits - Checking Each Position

Loop through all bits

4m

18. Count Bits - Brian Kernighan's Algorithm

Clever bit trick

5m

19. Count Bits - Implementation

The code

4m1 problems

20. Quiz: Counting Bits

Knowledge check

2m1 problems

21. Checking Power of Two

One-line trick

4m

22. Problem - Power of Two

LeetCode 231

2m1 problems

23. Power of Two - Implementation

The code

4m1 problems

24. Isolating the Rightmost Set Bit

Another useful trick

5m

25. XOR Properties

Magic of XOR

5m

26. Problem - Single Number

LeetCode 136

2m1 problems

27. Single Number - The Idea

XOR cancellation

4m

28. Single Number - Implementation

The code

4m1 problems

29. Quiz: XOR

Knowledge check

2m1 problems

30. Bitmasks for Subsets

Representing sets as bits

4m

31. Iterating Through Subsets

Generate all subsets

4m

32. Subset Operations

Union, intersection, difference

4m

33. Problem - Subsets

LeetCode 78

2m1 problems

34. Subsets - Bitmask Approach

Using bit manipulation

4m

35. Subsets - Implementation

The code

5m1 problems

36. Quiz: Bitmasks

Knowledge check

2m1 problems

37. Swapping Without a Temporary

XOR swap trick

4m

38. Reversing Bits

Mirror the bits

4m

39. Bit Manipulation Tricks Summary

Quick reference

4m

40. When to Use Bit Manipulation

Recognizing the pattern

5m

41. Section Recap

What we learned

2m

Ready to start learning?

Access all 41 lessons with interactive content and progress tracking.