In C++, operators are used to perform various operations on variables and values. Here are some of the most common types of operators in C++:
1. Arithmetic operators: Arithmetic operators are used to perform mathematical operations on numeric values. The following arithmetic operators are supported in C++:
– `+` (addition)
– `-` (subtraction)
– `*` (multiplication)
– `/` (division)
– `%` (modulus)
For example:
int x = 10; int y = 5; int z = x + y; // z is now 15 int w = x * y; // w is now 50 double q = x / y; // q is now 2.0 int r = x % y; // r is now 0
2. Relational operators: Relational operators are used to compare two values and return a Boolean value (true or false). The following relational operators are supported in C++:
– `==` (equal to)
– `!=` (not equal to)
– `<` (less than)
- `>` (greater than)
– `<=` (less than or equal to)
- `>=` (greater than or equal to)
For example:
int x = 10; int y = 5; bool a = x == y; // a is false bool b = x != y; // b is true bool c= x < y; // c is false bool d = x > y; // d is true bool e = x <= y; // e is false bool f = x >= y; // f is true
3. Logical operators: Logical operators are used to perform logical operations on Boolean values. The following logical operators are supported in C++:
– `&&` (logical AND)
– `||` (logical OR)
– `!` (logical NOT)
For example:
bool a = true; bool b = false; bool c = a && b; // c is false bool d = a || b; // d is true bool e = !a; // e is false
4. Bitwise operators: Bitwise operators are used to perform operations on individual bits of a value. The following bitwise operators are supported in C++:
– `&` (bitwise AND)
– `|` (bitwise OR)
– `^` (bitwise XOR)
– `
` (bitwise NOT) - `<<` (left shift) - `>>` (right shift) For example:
int a = 0b1010; // binary representation of 10
int b = 0b1100; // binary representation of 12
int c = a & b; // c is 0b1000 (8)
int d = a | b; // d is 0b1110 (14)
int e = a ^ b; // e is 0b0110 (6)
int f =
a; // f is 0b0101 (-11) int g = a << 1; // g is 0b10100 (20) int h = b >> 2; // h is 0b0011 (3)
There are other types of operators in C++ as well, such as assignment operators, increment and decrement operators, and conditional operators. Understanding these operators and how to use them is an important part of learning C++.