What you will learn
How binary representation and two's complement encode every signed 32-bit integer
The six bitwise operators (AND, OR, XOR, NOT, left shift, right shift) and what each does at the bit level
The kth bit pattern in all four of its forms: check, set, unset, and toggle
The full XOR family: cancellation, swapping, toggle counting, and odd-occurrence detection
Bitmasking as a way to enumerate every subset of N elements in O(N * 2^N) time
Where the bit view beats arithmetic: parity, powers of two, and fast exponentiation in O(log N)
Why this course
Most engineers treat bit manipulation as a bag of memorised tricks. n & (n - 1) clears the lowest set bit, n & -n isolates it, a ^ a cancels to zero. The recipes work until an interviewer asks you to find a missing number in O(1) space, generate every subset of an array, or count parity in constant time, and you cannot derive the move because you never understood why the trick worked in the first place.
This course rebuilds your bit-level thinking starting from binary representation and two's complement, then walks you through the six patterns that cover almost every bit manipulation question you will see in a coding interview. Across 21 problems you learn when XOR cancellation beats hashing, when a bitmask collapses a recursive enumeration into a single loop, and when the binary view turns an O(N) scan into an O(1) check.
Requirements
The course assumes you can read code in any mainstream language, but no prior bit-level experience is needed.
- Comfort writing loops, conditionals, and functions in at least one language.
- Familiarity with basic Big-O notation (O(1), O(N), O(log N)).
- Willingness to think in binary instead of decimal when reading a problem.
Overview
Bit manipulation is what your CPU does on every arithmetic operation. Every integer in memory is a fixed-width sequence of bits, and every +, *, or == your code runs decomposes into bit-level operations underneath. Most of the time the abstraction is invisible. But certain problems, counting parity in a network packet, encoding a subset as a single integer, finding a duplicate without extra memory, are solved naturally at the bit level and clumsily at any higher one. This course teaches you to see those problems and answer them in their native language.
Representation of bit manipulation
The course covers both halves of the problem: the bit-level operations themselves, and the six patterns that compose them into solutions.
Fundamentals
The course opens with the binary representation of integers. You see how a positive integer maps to its base-2 digits and how negative numbers are encoded under two's complement: flip every bit, then add one. Once that picture is clear, the meaning of the sign bit, why INT_MIN is -2^31 instead of -(2^31 - 1), and why XOR detects opposite signs all follow from the same layout.
From there the course covers the six bitwise operators: AND, OR, XOR, NOT, left shift, and right shift. Each operator has a truth table and each truth table has a use. AND masks bits, OR sets bits, XOR toggles bits and cancels duplicates, left shift multiplies by powers of two, and right shift divides. The course frames every later pattern as a composition of these six primitives. You build a mask, apply the right operator, then read the result.
The fundamentals close with the kth bit pattern, where the four core moves of bit manipulation appear together for the first time. Building a mask with 1 << (k - 1), you check a bit with AND, set it with OR, unset it with AND-NOT, and toggle it with XOR. Every later pattern in the course composes these four moves on a larger scale.
Problem solving
After the fundamentals, the rest of the course is patterns. Most bit manipulation problems you will see in an interview reduce to one of six templates. Once you can see the bit-level structure of the problem, the solution writes itself.
1 << (k - 1) then AND, OR, XOR, or AND-NOT to check, set, toggle, or unset the kth bit in O(1) time. Kth Bit Check, Set Kth Bit, Unset Kth Bit, Toggle Kth Bit.n & -n to isolate the rightmost set bit and a log to convert it to a position, locating set bits in O(1) time without a loop. Only Set Bit, Rightmost Set Bit.a ^ a = 0, a ^ 0 = a) to swap, toggle, and isolate elements that appear an odd number of times in O(N) time and O(1) space. Have Opposite Signs, Swap Numbers, Toggle Count, Odd Occurring Element, Odd Occurring Element II, Duplicate Element, Missing And Duplicated Elements.2^N masks to generate every subset of an N-element collection in O(N · 2^N) time. Pairwise Bits Swap, Unique Subsets.Each pattern starts with the simplest problem in the family and ramps up. The XOR pattern opens with Have Opposite Signs and Swap Numbers so you internalise the cancellation property on numeric input, then extends to Odd Occurring Element and finally Missing and Duplicated Elements where the same reasoning runs across arrays. The course closes with the Applications pattern, where parity, powers of two, and fast exponentiation show the bit view turning naive arithmetic into solutions you can derive on a whiteboard without notes.
How this course is different
Most bit manipulation material on the internet teaches the tricks without the reasoning. This course refuses to do that.
n & (n - 1) without explaining why the cancellation works on any number.Who this course is for
The course suits anyone who needs to understand bit manipulation rather than just memorise its tricks.
- New programmers who can write loops and conditionals but have never thought about what happens to bits when they compute
a + b. - Self-taught and bootcamp graduates who solve array problems comfortably but freeze when a question says "do this in O(1) space" or "without using a loop".
- Working developers who use bitwise operators in flag fields, feature toggles, or network code but cannot derive
n & (n - 1)from first principles. - Engineers preparing for FAANG and Big Tech interviews where bit manipulation appears in systems, embedded, and senior-level coding rounds.
- Returning engineers who learned binary years ago in a college course and want a refresher anchored in interview-grade problems.
- Anyone who has seen
^,&, and<<in code review and would rather understand them than skim past them.
Continue your DSA journey
Bit manipulation pairs naturally with several other topics in this curriculum. After this course, the natural next steps depend on where you want to deepen.
- Array: Bitmasking enumeration runs over arrays, and many XOR problems take arrays as input. The array course teaches the underlying patterns (two pointers, sliding window) that combine with bit tricks in the harder mixed problems.
- Hash table: For counting and odd-occurrence problems, hash tables are the alternative to XOR. This course shows when each one wins. XOR gives you O(1) extra space when elements pair up cleanly, hash tables handle the general case. Knowing both makes the choice in an interview instant.
- Recursion: Fast exponentiation, computing
num^nin O(log N) by splittingnalong its bits, is the canonical place where bit manipulation meets recursion. The recursion course gives you the call-stack picture that makes the divide-and-conquer over bits feel natural. - Backtracking: Subset and permutation generation can be done with backtracking recursion or with bitmask iteration. The two approaches solve the same problems with different tradeoffs in space and clarity, and seeing both makes you faster at picking the right one.
- Dynamic programming: Bitmask DP is one of the major DP genres, covering travelling salesman, set cover, and assignment problems. It depends entirely on the bitmask construction taught in this course, which DP material usually assumes you already know.
Frequently asked questions
for loop in any language and read Big-O notation like O(N), you have everything you need to follow along.a ^ a = 0: any number XORed with itself cancels to zero. Second, XOR is commutative and associative, so the order of the operations does not matter. If you XOR every element of the array together, every number that appears an even number of times pairs up and cancels, leaving only the odd-occurring number. The whole solution runs in O(N) time and O(1) space.