Study smarter with Fiveable
Get study guides, practice questions, and cheatsheets for all your subjects. Join 500,000+ students with a 96% pass rate.
Bitwise operators give you direct control over individual bitsโthe fundamental building blocks of all data in C. When you're tested on these concepts, you're really being evaluated on your understanding of binary representation, memory efficiency, and low-level data manipulation. These operators show up everywhere in systems programming: device drivers, embedded systems, network protocols, and graphics engines all rely heavily on bit-level operations.
Don't just memorize the symbols and syntax. Know why each operator exists and when to reach for it. Can you explain why AND is perfect for masking but useless for toggling? Do you understand why left-shifting is really just multiplication in disguise? That conceptual understanding is what separates students who ace exams from those who struggle with application questions.
These operators work by comparing corresponding bits between two operands. Each operator applies a different logical rule to determine the output bit.
&)5 & 3 yields 1 because 0101 & 0011 = 0001โonly the rightmost bit survives|)5 | 3 yields 7 because 0101 | 0011 = 0111โall "on" bits are preserved^)5 ^ 3 yields 6 because 0101 ^ 0011 = 0110โmatching bits cancel outCompare: AND (&) vs. OR (|)โboth combine two operands bit-by-bit, but AND is restrictive (narrows results) while OR is permissive (expands results). If an exam asks about "clearing bits," think AND with a mask; for "setting bits," think OR.
These operators modify a single value rather than combining two. They change the bit pattern through inversion or positional shifting.
~)~5 yields -6, not what you might expect from simple inversionx & ~mask) to clear specific bits<<)5 << 1 yields 10 because 0101 becomes 1010โeffectively >>)5 >> 1 yields 2 because 0101 becomes 0010โeffectively truncatedCompare: Left shift (<<) vs. Right shift (>>)โboth move bits positionally, but left shift multiplies (and can overflow) while right shift divides (and truncates). Remember: shifting by positions equals multiplying or dividing by .
These combine bitwise operations with assignment for cleaner, more efficient code. They modify a variable in place rather than creating a new value.
&=, |=, ^=, <<=, >>=)x &= y is equivalent to x = x & y, reducing redundancyCompare: x = x | mask vs. x |= maskโfunctionally identical, but the compound form is preferred in professional code for clarity and to avoid repeating the variable name (which matters when the variable is a complex expression).
These patterns combine basic operators to accomplish common programming tasks. Mastering these idioms is essential for efficient low-level programming.
1 << n produces a mask with only the th bit setx |= (1 << n) turns on a specific bit without affecting othersx &= ~(1 << n) turns off a specific bit using NOT to create an inverted maskx ^= (1 << n) flips a bit regardless of its current stateflags |= FLAG_A โ Check flag: (flags & FLAG_A) != 0int instead of 32 separate variablesCompare: Setting a bit (|=) vs. Clearing a bit (&= ~)โboth target specific bits, but setting uses OR with a 1-bit mask while clearing uses AND with an inverted mask. This is a classic FRQ topic: "Write code to clear the 3rd bit of variable x."
| Concept | Best Examples |
|---|---|
| Combining bits (both must be 1) | AND (&), &= |
| Combining bits (either can be 1) | OR (|), |= |
| Detecting differences | XOR (^), ^= |
| Inverting all bits | NOT (~) |
| Multiplication by powers of 2 | Left shift (<<), <<= |
| Division by powers of 2 | Right shift (>>), >>= |
| Isolating specific bits | Bitmasking with AND |
| Setting/clearing/toggling bits | Combined patterns with shifts |
Which two operators would you combine to clear the 4th bit of a variable? Why can't you use just one?
If x = 12 and y = 10, calculate x & y, x | y, and x ^ y. What does each result tell you about the relationship between the original bits?
Compare and contrast left shift and multiplication: when would x << 3 give a different result than x * 8?
You need to check whether a specific flag is set in a permissions variable. Which operator do you use, and what do you compare the result against?
Write the expression to toggle bit 5 of variable flags, then explain why XOR works for toggling but AND and OR don't.