In C++, operators are special symbols that perform operations on variables and values. Think of them as the action words of your programming sentences—they are essential for doing everything from basic math to making complex decisions. This guide breaks down the four most important types of operators: Arithmetic, Relational, Logical, and Bitwise, with simple examples and interactive challenges to help you master them.
🔹 Reference Table of C++ Operators
| Operator Type | Symbols | Purpose |
|---|---|---|
| Arithmetic | +, -, *, /, % | Perform mathematical calculations |
| Relational | ==, !=, <, >, <=, >= | Compare two values |
| Logical | &&, ||, ! | Combine or invert boolean expressions |
| Bitwise | &, |, ^, ~, <<, >> | Perform operations on individual bits |
1. Arithmetic Operators: The Calculators
These are the operators you learned about in math class. They allow you to perform basic calculations on numeric types like int and double.
#include <iostream>
int main() {
int a = 10;
int b = 3;
// Basic arithmetic operations
std::cout << "a + b = " << (a + b) << std::endl; // Addition
std::cout << "a - b = " << (a - b) << std::endl; // Subtraction
std::cout << "a * b = " << (a * b) << std::endl; // Multiplication
// Division of integers truncates the decimal
std::cout << "a / b = " << (a / b) << std::endl;
// Modulus operator gives the remainder
std::cout << "a % b = " << (a % b) << std::endl;
return 0;
}
Output
a + b = 13
a - b = 7
a * b = 30
a / b = 3
a % b = 1
📝 Try it Yourself: Write a program that asks the user for two decimal numbers (
double) and displays the result of their division. Does it truncate like with integers?
2. Relational Operators: The Comparators
Relational operators are used to compare two values. They are the foundation of decision-making in C++, as their result is always a boolean value: true or false.
#include <iostream>
int main() {
int score = 85;
int passing_score = 70;
std::cout << std::boolalpha; // Print "true" or "false" instead of 1 or 0
std::cout << "Is score equal to passing_score? " << (score == passing_score) << std::endl;
std::cout << "Is score not equal to passing_score? " << (score != passing_score) << std::endl;
std::cout << "Is score greater than passing_score? " << (score > passing_score) << std::endl;
std::cout << "Is score less than or equal to 100? " << (score <= 100) << std::endl;
return 0;
}
Output
Is score equal to passing_score? false
Is score not equal to passing_score? true
Is score greater than passing_score? true
Is score less than or equal to 100? true
📝 Try it Yourself: Create a program that asks for a user’s age and prints
trueif they are 18 or older, andfalseotherwise.
3. Logical Operators: The Decision Makers
Logical operators allow you to combine multiple boolean expressions to create more complex conditions.
&&(AND): Returnstrueonly if both conditions are true.||(OR): Returnstrueif at least one of the conditions is true.!(NOT): Inverts the boolean value (truebecomesfalse, andfalsebecomestrue).
#include <iostream>
int main() {
bool has_key = true;
bool knows_password = false;
std::cout << std::boolalpha;
// AND: Must have both to enter
std::cout << "Can enter with key AND password? " << (has_key && knows_password) << std::endl;
// OR: Can enter with either one
std::cout << "Can enter with key OR password? " << (has_key || knows_password) << std::endl;
// NOT: The opposite of has_key
std::cout << "Is the key missing? " << !has_key << std::endl;
return 0;
}
Output
Can enter with key AND password? false
Can enter with key OR password? true
Is the key missing? false
📝 Try it Yourself: Write a program that checks if a number is between 1 and 100 (inclusive). Ask the user for a number and print
trueorfalse.
4. Bitwise Operators: The Low-Level Manipulators
Bitwise operators work directly on the binary (base-2) representation of integers. They are powerful for low-level programming, such as controlling hardware, optimizing code, or working with flags.
#include <iostream>
#include <bitset> // To visualize binary representations
int main() {
unsigned int a = 5; // Binary: 00000101
unsigned int b = 9; // Binary: 00001001
std::cout << "a = " << std::bitset<8>(a) << std::endl;
std::cout << "b = " << std::bitset<8>(b) << std::endl;
// Bitwise AND: 1 only if both bits are 1
std::cout << "a & b = " << std::bitset<8>(a & b) << " (" << (a & b) << ")" << std::endl;
// Bitwise OR: 1 if at least one bit is 1
std::cout << "a | b = " << std::bitset<8>(a | b) << " (" << (a | b) << ")" << std::endl;
// Bitwise XOR: 1 only if bits are different
std::cout << "a ^ b = " << std::bitset<8>(a ^ b) << " (" << (a ^ b) << ")" << std::endl;
// Left Shift: Moves bits to the left (multiplies by 2)
std::cout << "a << 1 = " << std::bitset<8>(a << 1) << " (" << (a << 1) << ")" << std::endl;
// Right Shift: Moves bits to the right (divides by 2)
std::cout << "b >> 1 = " << std::bitset<8>(b >> 1) << " (" << (b >> 1) << ")" << std::endl;
return 0;
}
Output
a = 00000101
b = 00001001
a & b = 00000001 (1)
a | b = 00001101 (13)
a ^ b = 00001100 (12)
a << 1 = 00001010 (10)
b >> 1 = 00000100 (4)
📝 Try it Yourself: Write a program that uses the left shift operator (
<<) to calculate4 * 8(Hint: shifting left by 1 is multiplying by 2).
🔹 Frequently Asked Questions (FAQ)
Q1: What is the difference between the assignment operator (=) and the equality operator (==)?
A: This is a very common point of confusion for beginners. The single equals sign (=) is for assignment—it gives a variable a value (e.g., int x = 10;). The double equals sign (==) is for comparison—it checks if two values are equal and returns true or false (e.g., if (x == 10)).
Q2: Why does 10 / 3 result in 3 instead of 3.33?
A: In C++, when you divide two integers, the result is also an integer. The decimal part is truncated (cut off), not rounded. To get a decimal result, at least one of the numbers must be a floating-point type, like a double (e.g., 10.0 / 3).
Q3: What is the main difference between logical AND (&&) and bitwise AND (&)?
A: Logical AND (&&) operates on boolean (true/false) values to determine the logical outcome of a combined condition. Bitwise AND (&) operates directly on the individual bits of integer data. They are used for completely different purposes and should not be confused.
Q4: When would I ever use bitwise operators in real-world programming?
A: Bitwise operators are crucial in performance-critical areas. They are used in systems programming (like operating systems and drivers), embedded systems, networking protocols for manipulating data packets, and in game development for managing flags or optimizing graphics calculations.
Q5: What is operator precedence?
A: Operator precedence is the set of rules that determines the order in which operators are evaluated in a complex expression. For example, multiplication (*) has higher precedence than addition (+), so 5 + 2 * 3 is evaluated as 5 + 6, which equals 11. You can use parentheses () to override the default precedence.