Hello Readers,
Today, we’ll be going to discuss the operators in C++. In the previous tutorial, we discussed the data types in C++. Here is the link to practice C++ questions. Here is the link to the external documentation, if you want to have a look at it.
Operators are those symbols that are used to operate the variables and constants. C++ has a rich set of built-in operators. There are various types of operators:
- Arithmetic Operators
- Logical Operators
- Relational Operators
- Bitwise Operators
- Assignment Operators
- Miscellaneous Operators
C++ also has the facility of operator overloading, in which we overload the build-in or pre-defined operators. We’ll see operator overloading in the next upcoming tutorials.
Before starting with the explanation of types of operators, I would like to define a technical terminology here: Operand
Operand: Operand is a variable or constant which is used with the operator to get the desired result.
Arithmatic Operator in C++
An arithmetic operator is an operator which does arithmetic operations on the variables & constants. Below is the list of important arithmetic operators:
For Example: Let’s say A = 20 and B = 10
Operator | Description | Example |
+ | Adds 2 operands | A + B = 30 |
– | Subtract 2 operands | A – B = 10 |
* | Multiply 2 operands | A * B = 200 |
/ | Divides 2 operands | A / B = 2 |
% | Modulus Operator calculates the remainder | 0; Since B divides A completely |
++ | Increment Operator: Increments the integer by 1 | A++ = 21; |
— | Decrement Operator: Decreases the integer by 1 | A– = 19; |
Related: Modifiers and Datatypes in C++
Relational Operators in C++
Relational Operators are the operators which are used to check the relationship between 2 operands. There are the following relational operators:
For Example: A = 20, B = 10
Operator | Description | Example |
== | Returns true, if both operands are equal, otherwise, return false | A == B returns false |
!= | Returns false, of both operands, are equal, otherwise returns true | A != B returns true |
> | Returns true if the first operand is greater than second, otherwise, return false | A > B, returns True |
< | Returns false if the first operand is greater than the second, otherwise returns true | A < B, returns False |
>= | Returns true if the first operand is greater or equal to than second, otherwise returns false | A >= B returns true |
<= | Returns true if the first operand is smaller or equal to than second, otherwise returns true | A <= B returns false |
Related: Variables & its types in C++
Logical Operators in C++
Logical Operators are the operators, which are used to determine the logic between the operands. There are the following logical operators in C++:
For Example, A = 0, B = 1
Operator | Description | Example |
&& | AND Operator. This operator returns true if both the operands are not zero, otherwise returns false | A && B returns false |
|| | OR Operator. This operator returns false if any of the operands are non-zero, otherwise returns true | A || B returns true |
! | NOT Operator. This operator returns the opposite of the logical state of the operand. | ! A returns true |
Related: What to learn Basics of C++?
Bitwise Operator in C++
Bitwise operators are the operators which perform the bitwise operation on the operands.
P | Q | P & Q | P | Q | P ^ Q |
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 0 |
1 | 1 | 1 | 1 | 1 |
The above example is not showing all the bitwise operators. Below is a detailed description of all the bitwise operators supported in C++:
Assume A = 0011 1100
B = 0000 1101
Operator | Description | Example |
& | Performs bitwise AND on every bit of 2 numbers | A & B = 0000 1100, which is 12 |
| | Performs bitwise OR on every bit of 2 numbers | A | B = 0011 1101, which is |
^ | Performs XOR operation | A ^ B = 0011 0001 |
~ | Bitwise NOT. Inverts all the bits of a number | ~A = 11000011 |
<< | Left Shift. Takes 2 numbers, left shifts the bits of the first operand, & the number of shifts is decided by the second number | A >> 2 = 0000 1111 = 15 |
>> | Right Shift. Takes 2 numbers, right shifts the bits of the first operand, & the number of shifts is decided by the second number | A << 2 = 1111 0000 = 240 |
Related: HackerRank is important for placements
Assignment Operator in C++
Assignment variables are the operators, which are used to assign value to a variable. The value of the right side operand is assigned to the left side operand.
So, that’s all about the operators in C++. Hope you enjoy it and more importantly you understood it. If you have any doubts, you can post them in the comments section. Don’t forget to share it with your friends. The next tutorial will be out soon. Till then, stay tuned.