Topic 14 of 91 2. C++ Fundamentals Interactive Tutorial

C++ Operators Reference: Categories, Syntax, and Practical Usage

16 mins read Last updated: 26 Sep 2026 23,400 views

Direct Answer & Core Concept

C++ provides a comprehensive suite of operators: Arithmetic (+, -, *, /, %), Relational (==, !=, <, >, <=, >=, and the C++20 three-way spaceship operator <=>), Logical (!, &&, ||), Bitwise (&, |, ^, ~, <<, >>), Assignment (=, +=, -=), Increment/Decrement (++), Member Access (., ->), Pointer (*, &), and Special operators (sizeof, alignof, typeid, comma, ternary ? :). Operator precedence and associativity determine the grouping of operands in unparenthesized expressions.

Detailed Explanation & Mechanics

1. Operator Categories & Precedence Table:

Architecture Diagram
C++ OPERATOR PRECEDENCE (HIGHEST TO LOWEST):
┌──────┬────────────────────────┬────────────────────────────────────────┐
│ Level│ Operators              │ Category                               │
├──────┼────────────────────────┼────────────────────────────────────────┤
│ 1    │ ::                     │ Scope Resolution                       │
│ 2    │ () [] . -> ++ -- (post)│ Member access, function call, postfix  │
│ 3    │ ++ -- (pre) + - ! ~ * &│ Unary prefix, address-of, dereference  │
│ 4    │ .* ->*                 │ Pointer-to-member                      │
│ 5    │ * / %                  │ Multiplicative arithmetic              │
│ 6    │ + -                    │ Additive arithmetic                    │
│ 7    │ >                  │ Bitwise shift / Stream insertion       │
│ 8    │                     │ Three-way comparison (C++20 Spaceship) │
│ 9    │ <  >=              │ Relational comparisons                 │
│ 10   │ == !=                  │ Equality tests                         │
│ 11   │ &                      │ Bitwise AND                            │
│ 12   │ ^                      │ Bitwise XOR                            │
│ 13   │ |                      │ Bitwise OR                             │
│ 14   │ &&                     │ Logical AND (short-circuiting)         │
│ 15   │ ||                     │ Logical OR (short-circuiting)          │
│ 16   │ ?:                     │ Conditional Ternary                    │
│ 17   │ = += -= *= /= %= etc.  │ Assignment operators                   │
└──────┴────────────────────────┴────────────────────────────────────────┘

Runnable Code Example: C++ Operators Reference

C++20 / C++23
main.cpp
Run Online
#include <iostream>
#include <compare>

struct PacketHeader {
    int sequenceNumber;
    int priority;

    // C++20 Spaceship Operator for automatic 3-way comparisons
    auto operator<=>(const PacketHeader&) const = default;
};

int main() {
    int a = 12; // 0000 1100
    int b = 25; // 0001 1001

    std::cout << "Bitwise AND (a & b): " << (a & b) << "
";
    std::cout << "Bitwise OR  (a | b): " << (a | b) << "
";
    std::cout << "Bitwise XOR (a ^ b): " << (a ^ b) << "
";
    std::cout << "Shift Left  (a << 2): " << (a << 2) << "
";

    // Ternary operator
    int maxVal = (a > b) ? a : b;
    std::cout << "Max value: " << maxVal << "
";

    // Spaceship operator
    PacketHeader p1{101, 1};
    PacketHeader p2{102, 1};
    if (p1 < p2) {
        std::cout << "Packet 1 has lower sequence than Packet 2
";
    }
    return 0;
}

Common C++ Technical Interview Questions & Answers

Interview Prep
Q

Q: What is the C++20 three-way comparison (spaceship operator <=>) operator?

A: The `<=>` operator compares two objects in a single expression, returning `std::strong_ordering`, `std::weak_ordering`, or `std::partial_ordering` (less, equal, greater). Declaring it `= default` auto-generates all 6 relational operators (`<`, `<=`, `>`, `>=`, `==`, `!=`).
Q

Q: Why should parentheses be used even when operator precedence is known?

A: Complex operator interactions (such as bitwise operators mixed with comparisons) can lead to subtle bugs and reduce readability for teammates.

Key Takeaways, Modern Best Practices & Idioms

C++ Best Practices & Production Rules

1. Use explicit parentheses to make complex mathematical or bitwise expressions unambiguous.
2. Adopt the C++20 default spaceship operator <=> to implement complete class comparisons with minimal boilerplate.
3. Do not overload logical operators (&&, ||, ,) as custom overloads lose short-circuit evaluation semantics.
We value your privacy & transparency

OperateTools uses cookies and local storage to ensure basic tool functionality, preserve preferences, and analyze aggregated traffic. We never sell your personal data or index your document files. Learn more in our Privacy Policy.