In this C++ tutorial, we will discuss what are operators and how many types of operators are present in C++.
What are the Operators?
Operators are the special symbols we use in any programming language which are used to perform some kind of operation between two operands. An operator always requires some appropriate operands to operate. An operand could be any data or a variable.
Types of Operators in C++
In C++ we have 5 types of Operators:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operator
- Assignment Operator
1. Arithmetic Operators
Arithmetic operators in C++, similar to the arithmetic concepts. Arithmetic operators can only be applied to Numeric data types such as Integer, Float and double. In C++ we have 7 arithmetic operators.
Operator | Operator Name | Description | Example |
+ | Addition | This operator is used to add two operands | 30 + 40 = 70 |
- | Subtraction | This operator gives the difference between two operands | 30 – 20 = 10 20 – 30 = -10 |
* | Multiplication | Multiply two operands | a = 20; b= 30; a*b would be 600 |
/ | Division | Perform the division operation. | If a= 20 and b= 10 a/b would be 2 |
% | Module | This operator provides the remainder of the integer division | If a= 20 and b= 10 a%b would be 0 |
++ | Increment | Increase the value of operand by one | If a = 10 a++ would be 11 |
-- | Decrement | Decrement the value of operand by one | If a = 10 a-- would be 9 |
Note: To perform the arithmetic operation we need numeric data types.
2. Relational Operator
Relation operators used to tell the relation between two operands, operands could be of any data type. The relational operators give output in boolean value either true or False. In C++ we have 6 types of Relational Operators:
Operator | Operator Names | Description | Example |
== | Is equal to | It checks whether two operands are equal or not | (2 == 2) //true 2 == 2.0 //false |
!= | Not equal to | It checks whether two operands are not equal. | (2 != 3) //true. (2 !=2) //false |
> | Greater than | Check whether the left operand is greater than right operand | (3 > 2) //true. (2 > 3) //false |
< | Smaller than | It checks whether the value of left operand is smaller than right operand | (2 < 3) // true. (3 < 3 )//false |
>= | Greater than equal to | If the value of left operand is greater or equal to the right operand it will give true else false. | (3 >= 3) // true. (3 >= 4) //false |
<= | Smaller than Equal to | It checks whether the value of left operand is smaller or equal to the right operand | (1 <= 2) // true. (12 <= 3)//false |
3. Logical Operators
Logical Operators operate between two boolean values and return an answer in Boolean value, In C++ we have 3 types of local Operators.
Operator | Operator Name | Description | Example |
&& | AND | If both the operands are true then the result will be true else false | (true && false) // false (true && true) //true |
|| | OR | If both the operands are false then only it will show false else the answer would be true | (true || false) //true (false || false)// false |
! | NOT | It converts the false to true and true to false. | !(true) // false !(false) //true |
4. Bitwise Operator:
The bitwise operator first the operators into binary format and then operate on them. Example:
let a = 60 and b = 30
so, the binary form of 60 is 0011 1100 and binary form of 13 is 0000 1101 Then a & b would be =(0&0 0&0 1&0 1&0 1&1 1&1 0&0 0&0)= 0000 1100 = 12
Operator | Operator Name | Description |
& | Bit AND | Perform the AND operation between two binary operands |
| | Bit OR | Perform the OR operation between two binary operands |
^ | Bit XOR | Perform the XOR operation between two binary operands |
~ | Bit One's Complement | Flip the binary, change 1 to 0 and 0 to 1 |
<< | Left shift | Shift the binary number to left |
>> | Right Shift | It shifts the binary number to the right |
5. Assignment Operators
Assignment operators are used for assigning a value to the variable.
Operator | Operators Name | Description | Example |
= | Assignment | Assign the right value to the left variable | a = 20 |
+= | Add and assign | Add the right value in the left variable | a += 30 which is equivalent to a = a+30 |
-= | Subtract and assign | Subtract the right value from the left variable | a -= 30 which is equivalent to a = a-30 |
*= | Multiply and assign | Multiply the right value into the left variable | a *= 30 which is equivalent to a = a*30 |
/= | Divide and assign | Divide the right value from the left variable | a /= 30 which is equivalent to a = a/30 |
%= | Assign the module | Divide the right value from the left variable and assign the remainder to the left variable | a %= 30 which is equivalent to a = a%30 |
Summary
- Operators are the special symbols that perform a specific operation on operands
- Operator requires appropriate operands to perform the operation
- An operand could be a variable or data itself.
- In C++ we have 5 types of Operators Arithmetic, Logical, Relational, Bitwise and Assignment Operators.
- Arithmetic Operators operate on Numeric datatypes
- Relations Operators can Operate on Numerical as well as character datatypes and it gives output in a Boolean value
- Logical Operators operate on boolean values.
- Assignment operators used for assigning value to a variable.
People are also reading: