C++ Basics: Operators
Master arithmetic, comparison, logical, bitwise, and special operators and understand how precedence controls evaluation order.
Arithmetic Operators
Arithmetic operators perform mathematical calculations. Integer division truncates the fractional part; use floating-point operands when you need the decimal result.
Arithmetic Operators
The modulo operator % only works with integers and returns the remainder after division.
- + addition, - subtraction, * multiplication
- / division, integer division when both operands are integers
- % modulo: 10 % 3 = 1 (useful for wrapping, even/odd checks)
- 7 / 2 = 3 (integer), 7.0 / 2 = 3.5 (double)
Arithmetic Operators
C++All five operators applied to the same pair of integers.
Increment and Decrement Operators
++ adds 1 and -- subtracts 1. The prefix form changes the value before it is used; the postfix form changes it after.
++ and --
Prefer prefix (++i) over postfix (i++) when the return value is not needed, it avoids creating a temporary copy.
- ++i (prefix): increment first, then return new value
- i++ (postfix): return current value, then increment
- --i / i--: same rules apply for decrement
- Never write i++ + ++i in one expression, the order is undefined
Prefix vs Postfix Increment
C++Watch how the return value differs between postfix and prefix.
Assignment Operators
Compound assignment operators combine an arithmetic operation with assignment, making code more concise.
Assignment Operators
x += 5 is exactly equivalent to x = x + 5, compound operators are shorthand, not a different operation.
- = simple assignment
- +=, -=, *=, /=, %= compound assignment
- x *= 2 doubles x in place
- x %= 10 keeps only the last digit of x
Compound Assignment
C++Each operator modifies x in place.
Comparison Operators
Comparison operators compare two values and return a bool. They are the building blocks of conditions in if statements and loops.
Comparison Operators
The most common mistake is using = (assignment) instead of == (comparison) inside a condition.
- == equal to, != not equal to
- < less than, > greater than
- <= less than or equal, >= greater than or equal
- All return true or false
Comparison Operators
C++Each comparison returns a bool printed as true or false.
Logical Operators
Logical operators combine boolean expressions. C++ uses short-circuit evaluation: the right-hand side is not evaluated if the left-hand side already determines the result.
&&, ||, !
Short-circuit evaluation means if the left side of && is false, the right side is never evaluated, use this to guard against null checks.
- && AND: true only if both sides are true
- || OR: true if at least one side is true
- ! NOT: flips true to false and vice versa
- Short-circuit: false && anything = false; true || anything = true
Logical Operators
C++AND combines two conditions; NOT flips a boolean.
Bitwise Operators
Bitwise operators work on the individual bits of integer values. They are commonly used in embedded systems, flags, and performance-sensitive code.
Bitwise Operators
Left shift (<<) multiplies by 2 per shift; right shift (>>) divides by 2 per shift, both are faster than multiplication or division.
- & AND: bit is 1 only if both bits are 1
- | OR: bit is 1 if at least one bit is 1
- ^ XOR: bit is 1 if bits differ
- ~ NOT: flips all bits
- << left shift, >> right shift
Bitwise Operators
C++AND, OR, XOR, and left shift on two small integers.
Ternary Operator
The ternary operator condition ? valueIfTrue : valueIfFalse is a compact one-line alternative to a simple if-else.
? : Ternary Operator
Use the ternary operator for simple value selection; stick with if-else for anything involving statements or side effects.
- Syntax: condition ? expr1 : expr2
- Returns expr1 if condition is true, expr2 otherwise
- Can be used anywhere an expression is expected (assignments, function arguments)
- Deeply nested ternaries become hard to read, prefer if-else when more than one level
Ternary Operator
C++Select a string based on a numeric condition in one line.
sizeof Operator
sizeof returns the size in bytes of a type or variable. It is evaluated at compile time and returns a value of type size_t.
sizeof
Use sizeof to write portable code that does not hard-code type sizes, since sizes can differ between platforms.
- sizeof(int) is typically 4 on 64-bit systems
- sizeof(double) is typically 8
- sizeof(array) gives total bytes; divide by sizeof(element) for element count
- Evaluated at compile time, zero runtime overhead
sizeof Operator
C++Query sizes of types and compute array length portably.
Operator Precedence
When multiple operators appear in one expression, precedence rules determine which is evaluated first. When in doubt, use parentheses to make the intended order explicit.
Precedence Order (high to low)
Precedence determines grouping; associativity determines order when two operators have the same precedence.
- Highest: () parentheses, ++ -- (postfix)
- Then: ! ~ ++ -- (prefix), unary + -
- Then: * / %
- Then: + -
- Then: << >>
- Then: < <= > >=, then == !=
- Then: &, ^, |, &&, ||
- Lowest: = += -= (assignment), ?: ternary
Operator Precedence
C++Multiplication binds tighter than addition; parentheses override precedence.
Knowledge Check
1. What does the % operator compute?
2. What does x++ do?
3. Which operator checks equality in C++?
4. What does the ternary operator return?
5. What does the << bitwise operator do?
6. What does sizeof(int) return on a typical 64-bit system?