Operators in C++
In this tutorial, we will learn about operators in C++. We will cover various categories of operators and their usage.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, and division.
Operator | Description | Example |
---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus | a % b |
++ | Increment | a++ |
-- | Decrement | a-- |
Assignment Operators
Assignment operators are used to assign values to variables. They combine an arithmetic operation with assignment.
Operator | Description | Example | Same as |
---|---|---|---|
= | Assignment | a = b | a = b |
+= | Add and assign | a += b | a = a + b |
-= | Subtract and assign | a -= b | a = a - b |
*= | Multiply and assign | a *= b | a = a * b |
/= | Divide and assign | a /= b | a = a / b |
%= | Modulus and assign | a %= b | a = a % b |
&= | Bitwise AND and assign | a &= b | a = a & b |
|= | Bitwise OR and assign | a |= b | a = a | b |
^= | Bitwise XOR and assign | a ^= b | a = a ^ b |
<<= | Left shift and assign | a <<= b | a = a << b |
>>= | Right shift and assign | a >>= b | a = a >> b |
Comparison Operators
Comparison operators are used to compare two values. They return a boolean result: true or false.
Operator | Description | Example |
---|---|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
Logical Operators
Logical operators are used to perform logical operations on boolean values. They are commonly used in conditional statements.
Operator | Description | Example |
---|---|---|
&& | Logical AND | a && b |
|| | Logical OR | a || b |
! | Logical NOT | !a |
Bitwise Operators
Bitwise operators are used to perform bit-level operations on integer types. These operations are faster and closer to the hardware.
Operator | Description | Example |
---|---|---|
& | Bitwise AND | a & b |
| | Bitwise OR | a | b |
^ | Bitwise XOR | a ^ b |
~ | Bitwise NOT | ~a |
<< | Left shift | a << b |
>> | Right shift | a >> b |
Example 1: Arithmetic Operators Example
- Declare two integer variables
a
andb
. - Assign values to
a
andb
. - Perform addition, subtraction, multiplication, division, and modulus operations using
a
andb
. - Print the results of each operation.
C++ Program
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 5;
int add = a + b;
int subtract = a - b;
int multiply = a * b;
int divide = a / b;
int modulus = a % b;
cout << "Addition result: " << add << endl;
cout << "Subtraction result: " << subtract << endl;
cout << "Multiplication result: " << multiply << endl;
cout << "Division result: " << divide << endl;
cout << "Modulus result: " << modulus << endl;
return 0;
}
Output
Addition result: 15 Subtraction result: 5 Multiplication result: 50 Division result: 2 Modulus result: 0
Example 2: Assignment Operators Example
- Declare an integer variable
num
and assign a value. - Reset the value of
num
before each assignment operation. - Use assignment operators to perform various operations on
num
. - Print the result of each operation.
C++ Program
#include <iostream>
using namespace std;
int main() {
int num = 10;
cout << "Initial value of num: " << num << endl;
num += 5; // Addition assignment
cout << "After addition, num: " << num << endl;
num = 10; // Reset num
num -= 3; // Subtraction assignment
cout << "After subtraction, num: " << num << endl;
num = 10; // Reset num
num *= 2; // Multiplication assignment
cout << "After multiplication, num: " << num << endl;
num = 10; // Reset num
num /= 4; // Division assignment
cout << "After division, num: " << num << endl;
num = 10; // Reset num
num %= 3; // Modulus assignment
cout << "After modulus, num: " << num << endl;
num = 10; // Reset num
num &= 3; // Bitwise AND assignment
cout << "After bitwise AND, num: " << num << endl;
num = 10; // Reset num
num |= 3; // Bitwise OR assignment
cout << "After bitwise OR, num: " << num << endl;
num = 10; // Reset num
num ^= 3; // Bitwise XOR assignment
cout << "After bitwise XOR, num: " << num << endl;
num = 10; // Reset num
num <<= 2; // Left shift assignment
cout << "After left shift, num: " << num << endl;
num = 10; // Reset num
num >>= 1; // Right shift assignment
cout << "After right shift, num: " << num << endl;
return 0;
}
Output
Initial value of num: 10 After addition, num: 15 After subtraction, num: 7 After multiplication, num: 20 After division, num: 2 After modulus, num: 1 After bitwise AND, num: 2 After bitwise OR, num: 11 After bitwise XOR, num: 9 After left shift, num: 40 After right shift, num: 5
Example 3: Comparison Operators Example
- Declare two integer variables
x
andy
. - Compare the values of
x
andy
using comparison operators. - Print the comparison result.
C++ Program
#include <iostream>
using namespace std;
int main() {
int x = 5, y = 10;
cout << "x > y: " << (x > y) << endl; // Outputs 0 since x is not greater than y
cout << "x < y: " << (x < y) << endl; // Outputs 1 since x is less than y
cout << "x >= y: " << (x >= y) << endl; // Outputs 0 since x is not greater than or equal to y
cout << "x <= y: " << (x <= y) << endl; // Outputs 1 since x is less than or equal to y
cout << "x == y: " << (x == y) << endl; // Outputs 0 since x is not equal to y
cout << "x != y: " << (x != y) << endl; // Outputs 1 since x is not equal to y
return 0;
}
Output
x > y: 0 x < y: 1 x >= y: 0 x <= y: 1 x == y: 0 x != y: 1
Example 4: Logical Operators Example
- Declare two boolean variables
a
andb
. - Perform logical operations using logical operators.
- Print the result of each operation.
C++ Program
#include <iostream>
using namespace std;
int main() {
bool a = true, b = false;
cout << "a && b: " << (a && b) << endl; // Outputs 0 (false)
cout << "a || b: " << (a || b) << endl; // Outputs 1 (true)
cout << "!a: " << (!a) << endl; // Outputs 0 (false)
return 0;
}
Output
a && b: 0 a || b: 1 !a: 0
Example 5: Bitwise Operators Example
- Declare two integer variables
a
andb
. - Perform bitwise operations using bitwise operators.
- Print the result of each operation.
C++ Program
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 3;
cout << "a & b: " << (a & b) << endl; // Outputs 1
cout << "a | b: " << (a | b) << endl; // Outputs 7
cout << "a ^ b: " << (a ^ b) << endl; // Outputs 6
cout << "~a: " << (~a) << endl; // Outputs -6
cout << "a << 1: " << (a << 1) << endl; // Outputs 10
cout << "b >> 1: " << (b >> 1) << endl; // Outputs 1
return 0;
}
Output
a & b: 1 a | b: 7 a ^ b: 6 ~a: -6 a << 1: 10 b >> 1: 1