Operators, Expressions & Precedence
অপারেটর, এক্সপ্রেশন ও প্রাধান্য
1. Categories of Operators
- Arithmetic:
+ - * / % - Comparison:
== != < > <= >= <=> - Logical:
&& || ! - Bitwise:
& | ^ ~ << >> - Assignment:
= += -= *= /= %= &= |= ^= <<= >>= - Increment/Decrement:
++ --
2. Arithmetic — The Subtle Rules
#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
| Trick | Code | What it does |
|---|---|---|
| Check if even | (n & 1) == 0 | Faster than n % 2 |
| Power of 2 test | n && !(n & (n-1)) | True iff n has exactly one bit set |
| Multiply by 2 | n << 1 | Bit shift left |
| Divide by 2 | n >> 1 | Bit shift right (for unsigned) |
| Set bit i | n |= (1 << i) | OR a 1 into position i |
| Clear bit i | n &= ~(1 << i) | AND with everything-but-bit-i |
| Toggle bit i | n ^= (1 << i) | XOR flips it |
#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:
// 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:
#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):
::scope() [] -> .postfix++ --prefix,!, unary-,*,&* / %+ -<< >>< <= > >=== !=&,^,|&&,||?:ternary= += -= ...,comma
7. Practice Problems
- What's the value of
(5 + 3) * 2 - 1?✨ Show Answer
15
- What's
10 % 3? And-10 % 3?✨ Show Answer
10 % 3 = 1.-10 % 3 = -1(C++ truncates toward zero). - Use bitwise to check if
n=12is even.✨ Show Answer
(12 & 1) == 0→ true. - Compute
1 << 5.✨ Show Answer
32 (= 25).
- Set bit 3 of n=0.
✨ Show Answer
0 | (1 << 3) = 8. - 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. - Difference between
&and&&?✨ Show Answer
&= bitwise AND on bits.&&= logical AND with short-circuit on booleans. - 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.) - Compute
16 >> 2.✨ Show Answer
4 (16 / 4).
- What does
n & -ndo?✨ Show Answer
Isolates the lowest set bit. Used in Fenwick trees / BIT.
- Write
isPowerOf2(64). True or false?✨ Show Answer
True. 64 =
0100 0000; 63 =0011 1111; AND = 0. - 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. - What's the value of
!0? Of!42?✨ Show Answer
!0 = true (1);!42 = false (0). Any non-zero is truthy. - Count set bits in 13 manually.
✨ Show Answer
13 =
1101→ 3 set bits. - 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"; } - Predict:
3 < 4 < 5?✨ Show Answer
Evaluates left-to-right:
(3 < 4) = true (1), then1 < 5 = true. But this is not mathematical "3 < 4 < 5". Use3 < 4 && 4 < 5. - Use ternary to find max of a, b.
✨ Show Answer
int m = (a > b) ? a : b;— or usestd::max(a, b). - What does compound assignment
x += ymean?✨ Show Answer
Equivalent to
x = x + y, but evaluatesxonly once. Useful whenxis a complex expression likearr[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.