Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 4 of 10
Operators - Arithmetic, Logical, Bitwise & Ternary
C++ inherits all C operators and adds a few (scope `::`, member access `.*` and `->*`, `typeid`, `static_cast` family). Operator precedence in C++ is famously complex - when in doubt, use parentheses. Understanding integer promotion (how `char + char` yields `int`) prevents silent bugs.
Operators - Precedence, Promotion & Safe Casting
#include <iostream>
#include <cstdint>
int main() {
// ── Arithmetic ────────────────────────────────────────────
int a = 17, b = 5;
std::cout << a + b << "
"; // 22
std::cout << a - b << "
"; // 12
std::cout << a * b << "
"; // 85
std::cout << a / b << "
"; // 3 (integer division truncates!)
std::cout << a % b << "
"; // 2
double da = 17.0, db = 5.0;
std::cout << da / db << "
"; // 3.4 (floating-point division)
// ── Compound assignment ───────────────────────────────────
int x = 10;
x += 5; // x = 15
x -= 3; // x = 12
x *= 2; // x = 24
x /= 4; // x = 6
x %= 4; // x = 2
// ── Integer promotion - silent widening ───────────────────
uint8_t small = 200;
uint8_t other = 100;
auto sum = small + other; // deduced as int (promotoed!), not uint8_t
std::cout << "sum type promotion: " << sum << "
"; // 300 (not overflow)
// ── Bitwise operators ────────────────────────────────────
uint32_t flags = 0;
flags |= (1U << 3); // set bit 3
flags &= ~(1U << 3); // clear bit 3
flags ^= (1U << 2); // toggle bit 2
bool bit2 = (flags >> 2) & 1U; // test bit 2
std::cout << "bit2 = " << bit2 << "
";
// ── Logical operators (short-circuit evaluation) ──────────
int *ptr = nullptr;
if (ptr != nullptr && *ptr == 5) { // safe: *ptr not evaluated if ptr==null
std::cout << "value is 5
";
}
// ── Comparison ───────────────────────────────────────────
std::cout << (3 == 3) << "
"; // 1 (true)
std::cout << (3 != 4) << "
"; // 1 (true)
// ── Ternary operator ─────────────────────────────────────
int score = 85;
std::string grade = (score >= 90) ? "A" : (score >= 80) ? "B" : "C";
std::cout << grade << "
"; // B
// ── C++ named casts (prefer over C-style cast) ────────────
double pi = 3.14159;
int pi_i = static_cast<int>(pi); // 3 - explicit narrowing
long pi_l = static_cast<long>(pi_i); // widening - OK
// avoid: (int)pi ← C-style cast, no type safety
// ── sizeof, alignof ──────────────────────────────────────
std::cout << sizeof(double) << "
"; // 8
std::cout << alignof(double) << "
"; // 8 (alignment requirement)
return 0;
}Common Mistakes
- Integer division truncates - `17 / 5 = 3`, not `3.4`. If you need floating-point division, at least one operand must be `double`: `17.0 / 5` or `static_cast<double>(17) / 5`.
- C-style casts bypass safety - `(int*)ptr` can cast away const, convert between unrelated pointer types, etc. Always use `static_cast`, `const_cast`, `reinterpret_cast`, or `dynamic_cast` which make the intent explicit and are checkable.
- Short-circuit evaluation saves you - `ptr && *ptr == 5` is safe because `&&` won't evaluate the right side if `ptr` is null. But `ptr != nullptr & *ptr == 5` (single `&`) evaluates both sides - no short circuit, crashes if ptr is null.
Tip
Tip
Practice Operators Arithmetic Logical Bitwise Ternary in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Arithmetic, Bitwise, Logical Short-Circuiting, and Precedence Evaluation Order
Practice Task
Note
Practice Task - (1) Write a working example of Operators Arithmetic Logical Bitwise Ternary from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Operators Arithmetic Logical Bitwise Ternary is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cpp code.
Key Takeaways
- C++ inherits all C operators and adds a few (scope `::`, member access `.
- Integer division truncates - `17 / 5 = 3`, not `3.4`. If you need floating-point division, at least one operand must be `double`: `17.0 / 5` or `static_cast<double>(17) / 5`.
- C-style casts bypass safety - `(int*)ptr` can cast away const, convert between unrelated pointer types, etc. Always use `static_cast`, `const_cast`, `reinterpret_cast`, or `dynamic_cast` which make the intent explicit and are checkable.
- Short-circuit evaluation saves you - `ptr && *ptr == 5` is safe because `&&` won't evaluate the right side if `ptr` is null. But `ptr != nullptr & *ptr == 5` (single `&`) evaluates both sides - no short circuit, crashes if ptr is null.
Finished this lesson?