"Under every integer is a row of bits, and the CPU can twiddle them all at once, in a single instruction. Bit manipulation won't change your algorithm's big-O, but it can shrink constants dramatically and pack a whole set into one number."
The Operators
Bitwise operations act on the binary representation directly: AND (&) keeps bits set in both operands, OR (|) keeps bits set in either, XOR (^) keeps bits that differ, NOT (~) flips every bit, and the shifts (<<, >>) slide bits left or right (a left shift by 1 multiplies by 2). Each is a single, blazing-fast CPU instruction. They're the lowest-level tools in the whole quest, and a handful of patterns built from them show up constantly.
The Tricks Worth Knowing
- Power-of-two check:
n & (n - 1) == 0is true exactly when n has a single set bit — i.e. n is a power of 2. (Subtracting 1 flips the lowest set bit and everything below it.) - Count set bits / clear the lowest one:
n & (n - 1)removes the lowest set bit; loop until zero to count bits. - XOR's magic:
a ^ a == 0anda ^ 0 == a. So if every element appears twice except one, XOR-ing them all cancels the pairs and leaves the loner — found in O(n) time and O(1) space, no hash set needed.
The Bitmask: a Set in One Integer
The biggest idea: a single integer can represent a set of up to ~64 items, where bit i being 1 means 'item i is present.' Then set operations become bit operations — union is OR, intersection is AND, membership is a shift-and-AND, adding an element is OR with a shifted 1. This is compact and fast, and it's the backbone of bitmask DP, where the DP state is 'which subset of items have I used,' encoded as one integer. It's how problems with up to ~20 items and exponential subset-states still fit in memory and run.
The Readability Caveat
Bit tricks are seductive and can make code unreadable fast — a clever one-liner that takes ten minutes to decode is usually the wrong call. Reach for bit manipulation when it genuinely matters: a hot inner loop where constant factors count, flags and permission sets, or bitmask DP where it's the natural representation. Otherwise, prefer the clear version. Cleverness that no teammate (or future-you) can read is a cost, not a flex.