Operators, Expressions & Precedence

অপারেটর, এক্সপ্রেশন ও প্রাধান্য

Read: ~35 min 18 practice problems

1. Categories of Operators

  • Arithmetic: + - * / %
  • Comparison: == != < > <= >= <=>
  • Logical: && || !
  • Bitwise: & | ^ ~ << >>
  • Assignment: = += -= *= /= %= &= |= ^= <<= >>=
  • Increment/Decrement: ++ --

2. Arithmetic — The Subtle Rules

arith.cpp
#include <iostream>

int main() {
    std::cout << 7 / 2     << "\n";  // 3 (integer)
    std::cout << 7 % 2     << "\n";  // 1 (remainder)
    std::cout << 7.0 / 2   << "\n";  // 3.5
    std::cout << -7 / 2    << "\n";  // -3 (truncation towards zero)
    std::cout << -7 % 2    << "\n";  // -1
}

3. Bitwise Tricks

TrickCodeWhat it does
Check if even(n & 1) == 0Faster than n % 2
Power of 2 testn && !(n & (n-1))True iff n has exactly one bit set
Multiply by 2n << 1Bit shift left
Divide by 2n >> 1Bit shift right (for unsigned)
Set bit in |= (1 << i)OR a 1 into position i
Clear bit in &= ~(1 << i)AND with everything-but-bit-i
Toggle bit in ^= (1 << i)XOR flips it
bits.cpp
#include <iostream>

bool isPowerOf2(int n) {
    return n > 0 && (n & (n - 1)) == 0;
}

int main() {
    for (int i = 1; i <= 20; ++i) {
        if (isPowerOf2(i)) std::cout << i << " ";
    }
    std::cout << "\n";
}

4. Short-Circuit Evaluation

&& stops as soon as it finds a false operand; || stops as soon as it finds true. This isn't just optimization — it's how we guard:

guard.cpp
// Safe: if p is null, second part is never evaluated
if (p != nullptr && p->value > 0) {
    // ...
}

// Safe: if denominator is 0, division never happens
if (denom != 0 && numer / denom > 10) {
    // ...
}

5. Spaceship Operator <=> (C++20)

The three-way comparison operator returns less, equal, or greater in one shot:

spaceship.cpp
#include <iostream>
#include <compare>

int main() {
    auto r = (3 <=> 5);
    if (r < 0)      std::cout << "less\n";
    else if (r > 0) std::cout << "greater\n";
    else             std::cout << "equal\n";
}

6. Precedence Cheat Sheet

From highest to lowest (selected):

  1. :: scope
  2. () [] -> . postfix
  3. ++ -- prefix, !, unary -, *, &
  4. * / %
  5. + -
  6. << >>
  7. < <= > >=
  8. == !=
  9. &, ^, |
  10. &&, ||
  11. ?: ternary
  12. = += -= ...
  13. , comma
When in doubt — use parentheses. They cost nothing at runtime and save hours of debugging.

7. Practice Problems

  1. What's the value of (5 + 3) * 2 - 1?
    ✨ Show Answer

    15

  2. What's 10 % 3? And -10 % 3?
    ✨ Show Answer

    10 % 3 = 1. -10 % 3 = -1 (C++ truncates toward zero).

  3. Use bitwise to check if n=12 is even.
    ✨ Show Answer

    (12 & 1) == 0 → true.

  4. Compute 1 << 5.
    ✨ Show Answer

    32 (= 25).

  5. Set bit 3 of n=0.
    ✨ Show Answer

    0 | (1 << 3) = 8.

  6. Predict: int x=5; std::cout << x++ << " " << ++x;
    ✨ Show Answer

    This is undefined behavior before C++17 due to unsequenced modification. Don't write code like this. Even when defined, the order of evaluation of << arguments is implementation-defined here.

  7. Difference between & and &&?
    ✨ Show Answer

    & = bitwise AND on bits. && = logical AND with short-circuit on booleans.

  8. Use ^ (XOR) to swap two ints without a temp.
    ✨ Show Answer
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;

    (Cute, but in real code use std::swap.)

  9. Compute 16 >> 2.
    ✨ Show Answer

    4 (16 / 4).

  10. What does n & -n do?
    ✨ Show Answer

    Isolates the lowest set bit. Used in Fenwick trees / BIT.

  11. Write isPowerOf2(64). True or false?
    ✨ Show Answer

    True. 64 = 0100 0000; 63 = 0011 1111; AND = 0.

  12. Output of 5 < 3 || 2 + 2 == 4?
    ✨ Show Answer

    true — short circuit doesn't apply because the first operand is false; the second is evaluated.

  13. What's the value of !0? Of !42?
    ✨ Show Answer

    !0 = true (1); !42 = false (0). Any non-zero is truthy.

  14. Count set bits in 13 manually.
    ✨ Show Answer

    13 = 1101 → 3 set bits.

  15. Use std::popcount (C++20) to count bits.
    ✨ Show Answer
    popcount.cpp
    #include <iostream>
    #include <bit>
    int main() {
        std::cout << std::popcount((unsigned)13) << "\n";
    }
  16. Predict: 3 < 4 < 5?
    ✨ Show Answer

    Evaluates left-to-right: (3 < 4) = true (1), then 1 < 5 = true. But this is not mathematical "3 < 4 < 5". Use 3 < 4 && 4 < 5.

  17. Use ternary to find max of a, b.
    ✨ Show Answer

    int m = (a > b) ? a : b; — or use std::max(a, b).

  18. What does compound assignment x += y mean?
    ✨ Show Answer

    Equivalent to x = x + y, but evaluates x only once. Useful when x is a complex expression like arr[f(i)].

Summary

Master arithmetic, comparison, logical and bitwise operators. Bit manipulation gives you O(1) tricks like power-of-2 testing. Short-circuit evaluation enables safe pointer/divisor guards. The spaceship <=> operator (C++20) returns three-way ordering. Use parentheses when in doubt.

Next Module → I/O Streams: cin, cout, formatting.