Basic Bit Manipulation Questions
Seven short problems that lock in the operators you just learned. None are tricky — they each apply exactly one identity from the toolbox. The goal is fluency, not difficulty.
1. Check if a number is even or odd
A number is even iff its lowest bit is 0. That’s literally the binary definition of “even” — 2k ends in 0, 2k+1 ends in 1.
Why bother over x % 2 == 0? They generate identical machine code on every modern compiler. The bit version reads more directly if you’re already in “bit thinking” mode — and it never trips you up on negative numbers, where % has language-specific quirks.
2. Multiply or divide by a power of 2
int times8(int x) { return x << 3; } // x * 8
int divideBy4(int x) { return x >> 2; } // x / 4 (for non-negative x)For negative numbers, x >> k is arithmetic shift in most languages — it rounds toward negative infinity, which differs from C-style integer division (which truncates toward zero). -5 / 2 == -2 but -5 >> 1 == -3. Use shifts on non-negative integers or know your language’s rule.
3. Get the k-th bit
int getBit(int x, int k) {
return (x >> k) & 1;
}Shift bit k to position 0, mask everything else away.
4. Set the k-th bit
int setBit(int x, int k) {
return x | (1 << k);
}OR with a mask that has only bit k set.
5. Clear the k-th bit
int clearBit(int x, int k) {
return x & ~(1 << k);
}AND with a mask that has 0 at bit k and 1 everywhere else.
6. Toggle the k-th bit
int toggleBit(int x, int k) {
return x ^ (1 << k);
}XOR with 1 flips, XOR with 0 leaves alone.
7. Swap two integers without a temp
void swap(int& a, int& b) {
a ^= b;
b ^= a;
a ^= b;
}Three XORs. Cute, but never use this in real code — a normal swap (using a temp variable) is faster and doesn’t break when a and b are the same memory location.
Putting it together — a tiny “bit set” class
Stack a few of these and you have a working bitset:
A whole 32-element set of booleans, every operation in O(1), the entire structure fitting in four bytes. This is the kind of thing that makes bit manipulation worth learning — replacing a 32-byte boolean array with a 4-byte int isn’t just an optimization, it’s a transformation in how you think about data.
Ready for real problems? Head to Practice Questions.