Learn the vocabulary of directly operating on an integer's individual binary bits with AND, OR, XOR, and shifts.
0 / 5 completed
1 / 5
At standup, a dev mentions solving a problem by directly operating on an integer's individual binary bits, using operations like AND, OR, XOR, and shifts, instead of working with the number as a whole. What is this technique called?
Bit manipulation is exactly this: solving a problem by directly operating on an integer's individual binary bits using operations like AND, OR, XOR, and left or right shifts, rather than treating the number only as a single whole value. A hash collision is an unrelated hash-table concept about two keys sharing a bucket. This direct-bit-level approach is exactly why bit manipulation can pack, test, or toggle many independent flags inside a single machine word extremely cheaply.
2 / 5
During a design review, the team stores a large set of independent boolean flags by packing each one into a single bit of an integer, using bitwise AND and OR to test and set individual flags. Which capability does this provide?
Packing flags into the bits of a single integer provides extremely compact storage and fast per-flag operations, since dozens of independent boolean flags can fit into one machine word, and a single bitwise AND or OR operation can test or set any one of them directly. Storing each flag as its own separate boolean variable would use far more memory and lose the ability to manipulate several flags at once with a single operation. This compact packing is exactly why bit manipulation is common in performance-sensitive and memory-constrained code.
3 / 5
In a code review, a dev notices a feature checks whether a number is even by converting it to a string and inspecting its last character, instead of using a much simpler bitwise operation. What does this represent?
This is a missed bit-manipulation opportunity, since checking whether a number is even only requires testing its lowest bit with a single bitwise AND against one, which is both simpler to write and cheaper to run than converting the number to a string and inspecting a character of it. A cache eviction policy is an unrelated concept about discarded cache entries. This string-conversion pattern is exactly the kind of unnecessary overhead a reviewer flags once a direct bitwise check would answer the same question.
4 / 5
An incident report shows a hot code path that repeatedly tests several independent flags on every request ran slower than expected, because it converted a set of separate boolean variables to and from a string representation on every check instead of using bitwise operations on a packed integer. What practice would prevent this?
Packing the flags into a single integer and testing or setting them with bitwise AND and OR directly avoids the repeated string conversion entirely, which is exactly the fix for the slowdown described in this incident. Continuing to convert to and from a string representation on every single check regardless of how hot the path is is exactly what wasted so much time. This packed-integer, bitwise-operation approach is the standard fix once profiling shows flag-checking overhead as the actual bottleneck on a frequently-run path.
5 / 5
During a PR review, a teammate asks why the team reaches for bit manipulation to pack flags into a single integer instead of just using a plain array of separate boolean variables, given that an array is far more readable. What is the reasoning?
A packed integer is dramatically more compact than a plain array of separate boolean variables, and a single bitwise operation can test or set several flags at once instead of touching each variable individually, which matters when flags are numerous or checked extremely often. The tradeoff is that packed bits are less self-descriptive than clearly named boolean variables, making the code harder to read without comments or named bit constants explaining what each position means. This is exactly why bit manipulation is reserved for cases where the compactness and speed genuinely matter enough to justify the readability cost.