Basic Syntax
Operators
Arithmetic, logical, and bitwise operators
Interview: Essential for problem solving
C++ Operators
Operators form the core toolset for evaluating logical conditions, mathematical algorithms, and performing low-level bitwise manipulation. Understanding their precedence, associativity, and modern standard additions is key to writing bug-free C++ code.
Operator Groups
Logical & Short-Circuiting
Relies on short-circuit evaluation: in A && B, if A is false, B is skipped entirely.
&&||!Bitwise Operations
Essential for hardware registers, flags, and custom hashing structures.
&|^~<<>>C++20 Spaceship Operator (<=>)
Introduced in C++20, the three-way comparison operator <=> calculates all relational logic in a single instruction. It returns structured types (std::strong_ordering, std::partial_ordering) instead of simple boolean values, allowing the compiler to auto-synthesize all standard comparisons (<, >, <=, >=) from a single operator definition.
Order of Evaluation & Sequence Points
Precedence and associativity specify how the compiler groups sub-expressions, but not the execution order of the operands. Expressions modifying the same variable multiple times without sequence points (e.g. i = i++) trigger Undefined Behavior. Avoid stacking multiple modifications within a single code line.
Interview Corner
Q: How does short-circuit evaluation work, and what is its main safety use case?
A: In a logical statement, evaluating operands stops as soon as the outcome is certain. For instance, in if (ptr != nullptr && ptr->val > 10), if ptr is null, the compiler skips evaluating ptr->val, avoiding a null-pointer dereference crash.
Q: What is the C++20 spaceship operator, and how does it optimize class implementations?
A: The three-way comparison operator <=> returns whether a value is less than, equal, or greater than another. By defining a single operator<=> in a class and letting it default, the compiler automatically generates all six standard comparative operators (==, !=, <, >, <=, >=), saving lines of boilerplate code.
Common Pitfalls
- Precedence between bitwise and relational operators: Writing
if (val & mask == 0). Since==has higher precedence than&, it evaluates asval & (mask == 0). Always wrap bitwise logic in parenthesis. - Modifying same variable twice in a statement: Doing
func(x++, x)orarr[i] = i++has undefined evaluation ordering.
Best Practices
- Always group complex bitwise operations explicitly using parentheses:
(value & flag) == flag. - Utilize standard logical operators (
&&, ||) to guard pointer accesses and array boundaries in conditional checks. - Default comparison rules: use C++20
operator<=>on custom types to minimize error-prone comparison boilerplates.
Use Cases
Bitwise Masking: Packing network headers, setting microcontroller pin states, and storing low-overhead boolean state arrays.
Short-Circuit Checks: Protecting complex pointer-chain traversals or indexing bounds in logical evaluations.
Spaceship Operator: Implementing clean, sorting-capable models in data structures and containers.
Common Mistakes
Forgetting that bitwise AND/OR has lower precedence than comparisons.
Side-effects in logical conditions (e.g. changing variables inside logical checks).
Triggering order of evaluation undefined behaviors in complex expressions.