Core Module
12 min forge
C++ Operators
Arithmetic, relational, logical, and bitwise operators in C++.
C++ Operators
π What is it
Operators are symbols used to perform operations on variables and values. C++ supports arithmetic ($+, -, *, /, %$), relational ($==, !=, >, <$), logical ($&&, ||, !$), and bitwise ($&, |, \sim, \wedge, <<, >>$) operators.
β‘ When to use
For calculations, comparisons, and boolean logic in conditional statements.
π§ Time complexity
- Basic arithmetic/logical: $O(1)$
- Bitwise: $O(1)$
π» Code example
cpp Standardint a = 10, b = 20; bool result = (a < b) && (b > 15); // Logical AND int bitwise = (a << 1); // Left shift (multiplies by 2)
β Common mistakes
- Confusing
=(assignment) with==(comparison). - Integer division:
5 / 2is2, not2.5. Use5.0 / 2for floating point results. - Operator precedence: Not using parentheses
()to clarify complex expressions.