Beginner⏱️ 8 min📘 Topic 6 of 32

➕ C++ Operators — Arithmetic, Logical, Relational and Bitwise

Complete guide to C++ operators — arithmetic, relational, logical, bitwise, assignment and ternary. Learn precedence, short-circuit evaluation and common pitfalls.

Operators are symbols that perform operations on values. C++ has more than most languages.

➕ Arithmetic

+ - * / % ++ -- — note % is the modulo (remainder).

🟰 Relational

== != < > <= >= — return bool.

🔁 Logical

&& (AND), || (OR), ! (NOT). They short-circuitA && B doesn't evaluate B if A is false.

🔬 Bitwise (works on the bits of integers)

& AND, | OR, ^ XOR, ~ NOT, << left shift, >> right shift.

⚡ Assignment compounds

+= -= *= /= %= &= |= ^= <<= >>=x += 5 is x = x + 5.

❓ Ternary (conditional expression)

int max = (a > b) ? a : b;

💡 When in doubt, parenthesize.

💻 Code Examples

Modulo for even/odd check

int n = 7;
if (n % 2 == 0) std::cout << "even";
else              std::cout << "odd";
Output:
odd

Short-circuit evaluation

int* p = nullptr;
if (p != nullptr && *p == 5) {
  // safe — *p only evaluated if p is not null
}
Output:
Avoids dereferencing a null pointer.

Bitwise — fast power-of-2 multiply

int x = 6;
int y = x << 2;   // multiplies by 4 (shift left by 2)
std::cout << y;
Output:
24

⚠️ Common Mistakes

  • Using `=` instead of `==` in conditions — `if (x = 5)` ASSIGNS 5 and is always true.
  • Forgetting precedence: `a + b * c` is `a + (b * c)`, not `(a + b) * c`.
  • Using `&` (bitwise) when you meant `&&` (logical) — both compile but behave differently.
  • Modulo with negative operands behaves differently in older C++ standards — pre-C++11 was implementation-defined.

🎯 Interview Questions

Real questions asked at top product and service-based companies.

Q1.What's the difference between `++x` and `x++`?Beginner
Both increment x by 1. The difference is the value of the EXPRESSION: `++x` returns the new value (incremented first), `x++` returns the old value (incremented after). Inside a loop standalone, they're identical.
Q2.What is the modulo operator and what does it do?Beginner
`%` returns the REMAINDER of integer division. `7 % 3 == 1`. Common uses: even/odd check, wrapping around (clock arithmetic), cycling through array indices.
Q3.What is short-circuit evaluation?Intermediate
For `&&`: if the left operand is false, the right operand is NOT evaluated. For `||`: if the left is true, the right is skipped. Used to safely guard expressions like `if (p && *p == 5)`.
Q4.When would you use bitwise operators?Intermediate
Flags / bitmasks (storing 32 booleans in a single int), fast power-of-2 multiplication (`x << 1` doubles), hashing, low-level protocols, graphics/embedded systems.
Q5.What does `(a > b) ? a : b` do, and when is it preferred over if/else?Advanced
It's the ternary conditional — returns a if a > b, else b. Preferred for short value-returning expressions in initialization or arguments; if/else is clearer for multiple statements or complex logic.

🧠 Quick Summary

  • Arithmetic: + - * / %, relational return bool.
  • && and || short-circuit — leverage this for safety checks.
  • Bitwise operates on bits: &, |, ^, ~, <<, >>.
  • Compound assigns: x += 5 is concise.
  • Use parentheses to make precedence explicit.