Enhanced Bitwise Operators Study Guide
A learner-friendly rewrite with explanations, memory tricks, practical examples, and interview notes.
1. Why Bitwise Operations Matter
Computers store everything as bits (0s and 1s). Bitwise operators let you manipulate those bits
directly. They are heavily used in embedded systems, device drivers, communication protocols,
optimization, and interview problems. Key Idea: • AND (&) = keep bits • OR (|) = turn bits on • XOR
(^) = flip bits • NOT (~) = invert bits • << = multiply by powers of 2 • >> = divide by powers of 2
2. AND Operator (&)
Think of AND as a filter. A bit survives only when both the value and mask contain 1. Example:
10101100 &00001111 ---------- 00001100 Uses: • Masking • Extracting fields from registers •
Checking flags • Even/Odd detection Interview Tip: n & (n-1) removes the lowest set bit.
3. OR Operator (|)
OR is used to turn bits ON. Once a bit becomes 1 through OR, it cannot be cleared. Uses: • Set a
bit • Combine flags • Configure registers • Build bitmasks
4. XOR Operator (^)
XOR produces 1 when bits are different. Memory Trick: Same -> 0 Different -> 1 Powerful
Properties: • a ^ a = 0 • a ^ 0 = a • a ^ b ^ b = a Applications: • Toggle bits • Find unique element in
an array • Parity checking • Detect changes between values
5. NOT (~)
NOT flips every bit. Example: 00001111 -> 11110000 Main Use: Create inverted masks for clearing
bits.
6. Shift Operators
Left Shift (<<): Moves bits left and multiplies by 2^n. Right Shift (>>): Moves bits right and divides by
2^n. Embedded Uses: • Build masks • Pack data into registers • Extract fields from messages
7. Interview Cheat Sheet
Power of 2: (n > 0) && ((n & (n-1)) == 0) Count Set Bits: while(n){ n &= (n-1); count++; } Isolate
Lowest Set Bit: n & (-n) Even/Odd: n & 1
8. Embedded Systems Patterns
Read-Modify-Write Pattern: REG = (REG & ~MASK) | VALUE; Common Uses: • GPIO
configuration • UART flags • SPI/CAN frame packing • Interrupt handling