What is the difference between && and &?
&& is logical AND (short-circuit); & is bitwise AND on integer bits.
Master all C++ operators: arithmetic, relational, logical, bitwise, assignment, and special operators. Learn operator precedence, usage examples, and best practices.
Math Operations
Comparison
Boolean Logic
Bit Operations
Operators express calculations and decisions. This lesson covers every major operator family, evaluation order, and shortcuts that make C++ concise—but easy to misuse.
Operator precedence bugs and short-circuit logic trip up beginners and appear in MCQ tests. Mastery here simplifies conditionals and loops.
Operators are special symbols that perform operations on variables and values. C++ provides a rich set of operators that can be classified into several categories based on their functionality.
Operators work with operands to form expressions: result = operand1 operator operand2
int a = 10, b = 3;
int sum = a + b; // 13int a = 10, b = 3;
int rem = a % b; // 1int a = 10, b = 3;
bool eq = (a == b); // falseint a = 10, b = 3;
bool gt = (a > b); // truebool x = true, y = false;
bool r = x && y; // falsebool x = true;
bool r = !x; // falseThe following table provides a comprehensive reference of all C++ operators with examples and descriptions:
| Category | Operator | Name | Description & Example |
|---|---|---|---|
| Arithmetic Operators | |||
| Arithmetic | + | Addition | Adds two operands: a + b |
| Arithmetic | - | Subtraction | Subtracts second operand: a - b |
| Arithmetic | * | Multiplication | Multiplies two operands: a * b |
| Arithmetic | / | Division | Divides numerator by denominator: a / b |
| Arithmetic | % | Modulus | Remainder after division: a % b |
| Arithmetic | ++ | Increment | Increases value by 1: a++ (post), ++a (pre) |
| Arithmetic | -- | Decrement | Decreases value by 1: a-- (post), --a (pre) |
| Relational/Comparison Operators | |||
| Relational | == | Equal to | Checks equality: a == b returns true if equal |
| Relational | != | Not equal to | Checks inequality: a != b returns true if not equal |
| Relational | > | Greater than | a > b returns true if a is greater |
| Relational | < | Less than | a < b returns true if a is less |
| Relational | >= | Greater than or equal | a >= b returns true if a is greater or equal |
| Relational | <= | Less than or equal | a <= b returns true if a is less or equal |
| Logical Operators | |||
| Logical | && | Logical AND | a && b returns true if both are true |
| Logical | || | Logical OR | a || b returns true if either is true |
| Logical | ! | Logical NOT | !a returns true if a is false |
| Bitwise Operators | |||
| Bitwise | & | Bitwise AND | Sets each bit to 1 if both bits are 1: a & b |
| Bitwise | | | Bitwise OR | Sets each bit to 1 if either bit is 1: a | b |
| Bitwise | ^ | Bitwise XOR | Sets each bit to 1 if only one bit is 1: a ^ b |
| Bitwise | ~ | Bitwise NOT | Inverts all bits: ~a |
| Bitwise | << | Left shift | Shifts bits left, fills with 0: a << n |
| Bitwise | >> | Right shift | Shifts bits right: a >> n |
| Assignment Operators | |||
| Assignment | = | Simple assignment | Assigns right operand to left: a = b |
| Assignment | += | Add AND assign | a += b is same as a = a + b |
| Assignment | -= | Subtract AND assign | a -= b is same as a = a - b |
| Assignment | *= | Multiply AND assign | a *= b is same as a = a * b |
| Assignment | /= | Divide AND assign | a /= b is same as a = a / b |
| Assignment | %= | Modulus AND assign | a %= b is same as a = a % b |
| Assignment | &= | Bitwise AND assign | a &= b is same as a = a & b |
| Assignment | |= | Bitwise OR assign | a |= b is same as a = a | b |
| Assignment | ^= | Bitwise XOR assign | a ^= b is same as a = a ^ b |
| Assignment | <<= | Left shift AND assign | a <<= n is same as a = a << n |
| Assignment | >>= | Right shift AND assign | a >>= n is same as a = a >> n |
| Special Operators | |||
| Special | ?: | Ternary/Conditional | condition ? expr1 : expr2 - If condition true, expr1 else expr2 |
| Special | , | Comma | expr1, expr2 - Evaluates both, returns expr2 |
| Special | sizeof | Size of | sizeof(type) - Returns size in bytes |
| Special | typeid | Type identification | typeid(expr) - Returns type information |
| Special | static_cast | Static cast | static_cast<type>(expr) - Compile-time type conversion |
| Special | dynamic_cast | Dynamic cast | dynamic_cast<type>(expr) - Runtime type conversion (polymorphic) |
| Special | const_cast | Const cast | const_cast<type>(expr) - Adds/removes const qualifier |
| Special | reinterpret_cast | Reinterpret cast | reinterpret_cast<type>(expr) - Reinterprets bit pattern |
| Special | . and -> | Member access | obj.member (direct), ptr->member (via pointer) |
| Special | :: | Scope resolution | Class::member or namespace::member |
| Special | new | Dynamic memory allocation | new type - Allocates memory on heap |
| Special | delete | Dynamic memory deallocation | delete ptr - Frees heap memory |
| Special | () | Function call | function(arg1, arg2) - Calls a function |
| Special | [] | Array subscript | array[index] - Accesses array element |
int a = 15, b = 4;
cout << a + b; // 19int a = 15, b = 4;
cout << a / b; // 3int a = 15, b = 4;
cout << a % b; // 3double x = 15.0, y = 4.0;
cout << x / y; // 3.75int c = 5;
cout << c++; // prints 5, c=6int c = 5;
cout << --c; // prints 4int age = 25;
bool ok = (age >= 18); // trueint age = 25;
bool diff = (age != 30); // trueint age = 25;
bool lic = true;
bool drive = (age >= 18) && lic;int age = 25;
bool elig = (age > 21) || (salary > 40000);int age = 25;
bool bad = !(age > 0); // falseint x = 5, y = 0;
bool safe = (y != 0) && (x / y > 2);unsigned char a = 53, b = 15;
int r = a & b; // 5unsigned char a = 53, b = 15;
int r = a | b; // 63unsigned char a = 53, b = 15;
int r = a ^ b; // 58unsigned char a = 53;
int r = a << 2; // 212int flags = 0;
const int FLAG_A = 1 << 0;
flags |= FLAG_A;int flags = 1;
const int FLAG_B = 1 << 1;
flags ^= FLAG_B;Operator precedence determines the order of evaluation in expressions. When operators have the same precedence, associativity determines the direction of evaluation.
Use parentheses () to explicitly specify evaluation order when in doubt. This improves readability and prevents bugs.
| Precedence | Category | Operators | Associativity |
|---|---|---|---|
| 1 (Highest) | Scope resolution | :: |
Left to right |
| 2 | Postfix | () [] -> . ++ -- |
Left to right |
| 3 | Unary | ++ -- + - ! ~ (type) * & sizeof |
Right to left |
| 4 | Member access | .* ->* |
Left to right |
| 5 | Multiplicative | * / % |
Left to right |
| 6 | Additive | + - |
Left to right |
| 7 | Bitwise shift | << >> |
Left to right |
| 8 | Relational | < <= > >= |
Left to right |
| 9 | Equality | == != |
Left to right |
| 10 | Bitwise AND | & |
Left to right |
| 11 | Bitwise XOR | ^ |
Left to right |
| 12 | Bitwise OR | | |
Left to right |
| 13 | Logical AND | && |
Left to right |
| 14 | Logical OR | || |
Left to right |
| 15 | Conditional | ?: |
Right to left |
| 16 | Assignment | = += -= *= /= %= &= ^= |= <<= >>= |
Right to left |
| 17 (Lowest) | Comma | , |
Left to right |
int a = 5, b = 10, c = 15;
int r = a + b * c; // 155int a = 5, b = 10, c = 15;
int r = a * b + c; // 65int a = 5, b = 10, c = 15;
bool r = a > b && b < c; // falseint a = 5, b = 10, c = 15;
int r = (a + b) * c; // 225int a = 5, b = 10, c = 15;
bool r = (a > b) && (b < c);int a = 5, b = 10, c = 15;
int r = a * (b + c); // 125x++ + ++x). The result is undefined and varies between compilers.
condition ? expr1 : expr2 - Compact if-else replacement.
int max = (a > b) ? a : b;
string result = (score >= 50)
? "Pass" : "Fail";
Returns size in bytes of type or variable.
sizeof(int); // 4 (usually)
sizeof(double); // 8 (usually)
int arr[10];
sizeof(arr); // 40 (if int is 4 bytes)
C++ offers four casting operators for type conversion.
// Static cast (compile-time)
double d = 3.14;
int i = static_cast(d);
// Dynamic cast (runtime, polymorphic)
Base* b = new Derived();
Derived* d = dynamic_cast(b);
Dynamic memory allocation and deallocation.
// Single object
int* ptr = new int(42);
delete ptr;
// Array
int* arr = new int[10];
delete[] arr;
int age = 20;
string st = (age >= 18) ? "Adult" : "Minor";cout << sizeof(int); // often 4
cout << sizeof(double); // often 8int arr[10];
cout << sizeof(arr); // 40 if int=4int value = 100;
{
int value = 200;
cout << ::value; // 100
}struct Point { int x, y; };
Point p = {10, 20};
cout << p.x;struct Point { int x, y; };
Point p = {10, 20};
Point* ptr = &p;
cout << ptr->y;const with pointers to prevent modificationsizeof on pointer types for array sizeif (x = 5) instead of if (x == 5) (assignment vs comparison)a < b < c instead of (a < b) && (b < c)5/2 = 2 not 2.5! (logical NOT) instead of ~ (bitwise NOT)&& and || use short-circuit evaluationint r = (2 + 3) * 4; // 20, not 11int total = 0;
total += 5;
total *= 2; // total = 10int num = 10, den = 0;
if (den != 0)
cout << num / den;int x = 5, y = 10;
if (x == y) // compare
cout << "Equal";int x = 3;
if (x > 0 && x < 5)
cout << "In range";double pi = 3.14159;
int n = static_cast<int>(pi); // 3&& is logical AND (short-circuit); & is bitwise AND on integer bits.
Postfix i++ returns the old value then increments; prefix ++i increments then returns the new value.
Scope, postfix increment, and unary operators rank high; consult a precedence table when in doubt.