Operators
Operators are symbols that tell the compiler to perform specific mathematical, relational, or logical operations. Understanding operators is crucial for writing effective programs that can manipulate data and make decisions.
Arithmetic Operators
These operators perform basic mathematical operations:
Basic Operations
- + Addition
- - Subtraction
- * Multiplication
- / Division
- % Modulus (remainder)
Increment/Decrement
- ++ Increment by 1
- -- Decrement by 1
int a = 10, b = 3;
cout << "Addition: " << a + b << endl; // 13
cout << "Subtraction: " << a - b << endl; // 7
cout << "Multiplication: " << a * b << endl; // 30
cout << "Division: " << a / b << endl; // 3 (integer division)
cout << "Modulus: " << a % b << endl; // 1
int x = 5;
cout << "x++: " << x++ << endl; // Prints 5, then x becomes 6
cout << "++x: " << ++x << endl; // x becomes 7, then prints 7
Modular Arithmetic Properties:
- (a + b) mod m = (a mod m + b mod m) mod m
- (a − b) mod m = (a mod m − b mod m) mod m
- (a · b) mod m = (a mod m · b mod m) mod m
Assignment Operators
These operators assign values to variables:
int x = 10;
x += 5; // x = x + 5; (x becomes 15)
x -= 3; // x = x - 3; (x becomes 12)
x *= 2; // x = x * 2; (x becomes 24)
x /= 4; // x = x / 4; (x becomes 6)
x %= 5; // x = x % 5; (x becomes 1)
cout << "Final value of x: " << x << endl; // 1
Comparison Operators
These operators compare values and return true or false:
int a = 10, b = 20;
cout << "a == b: " << (a == b) << endl; // false (0)
cout << "a != b: " << (a != b) << endl; // true (1)
cout << "a < b: " << (a < b) << endl; // true (1)
cout << "a > b: " << (a > b) << endl; // false (0)
cout << "a <= b: " << (a <= b) << endl; // true (1)
cout << "a >= b: " << (a >= b) << endl; // false (0)
Comparison Operators:
- == Equal to
- != Not equal to
- < Less than
- > Greater than
- <= Less than or equal to
- >= Greater than or equal to
Logical Operators
These operators perform logical operations and are used to combine or modify boolean expressions:
bool p = true, q = false;
cout << "p && q: " << (p && q) << endl; // false (logical AND)
cout << "p || q: " << (p || q) << endl; // true (logical OR)
cout << "!p: " << (!p) << endl; // false (logical NOT)
// Practical example
int age = 25;
bool hasLicense = true;
bool canDrive = (age >= 18) && hasLicense;
cout << "Can drive: " << canDrive << endl; // true
Short-Circuit Evaluation:
In logical AND (&&), if the first condition is false, the second condition is not evaluated. In logical OR (||), if the first condition is true, the second condition is not evaluated.
Bitwise Operators
These operators work at the bit level and are useful for low-level programming and optimization:
int a = 5; // Binary: 101
int b = 3; // Binary: 011
cout << "a & b: " << (a & b) << endl; // 1 (AND)
cout << "a | b: " << (a | b) << endl; // 7 (OR)
cout << "a ^ b: " << (a ^ b) << endl; // 6 (XOR)
cout << "~a: " << (~a) << endl; // -6 (NOT)
cout << "a << 1: " << (a << 1) << endl; // 10 (left shift)
cout << "a >> 1: " << (a >> 1) << endl; // 2 (right shift)
Operator Precedence
When multiple operators appear in an expression, they are evaluated in a specific order:
int result = 2 + 3 * 4; // Multiplication first: 2 + 12 = 14
cout << result << endl; // 14
// Use parentheses to change precedence
result = (2 + 3) * 4; // Addition first: 5 * 4 = 20
cout << result << endl; // 20
// Complex expression
result = 10 + 5 * 2 - 3 / 3; // 10 + 10 - 1 = 19
cout << result << endl; // 19
Precedence Order (High to Low):
- Parentheses ()
- Unary operators (++, --, !)
- Multiplication, Division, Modulus (*, /, %)
- Addition, Subtraction (+, -)
- Comparison operators (<, >, <=, >=)
- Equality operators (==, !=)
- Logical AND (&&)
- Logical OR (||)
- Assignment operators (=, +=, -=, etc.)
CodeForces Challenge Problems
Practice using different operators with these problems:
Theatre Square
Use arithmetic operators to calculate how many flagstones are needed to pave a square.
Solve ProblemOperator Tips
- Use parentheses to make your intentions clear
- Be careful with integer division - it truncates decimals
- Remember that comparison operators return boolean values
- Understand operator precedence to avoid bugs