Operators are the set of symbols that are used in a programming language to perform mathematical and logical operations.
There are mainly five types of operators present in any language. Here we’ll be discussing operators specific to C/C++ language.
Let’s dive deep and understand each category separately.
1). Arithmetic Operators
These operators are used to perform mathematical operations such as addition, multiplication, division, increment/decrement, subtraction and modulus.
The values on which operators works are called operands.
In the following article, let’s take two operands namely x & y.
Arithmetic Operators | |||
Operation | Symbol | Usage | Explanation |
Addition | + | x + y | Adds x and y, irrespective of their placement |
Subtraction | – | x – y | Subtracts the right operand (y) from the left (x) |
Multiplication | * | x * y | Multiplies a and y, irrespective of their placement |
Division | / | x / y | Divides left operator (x) by right operator (y) and returns the quotient. |
Increment | ++ | x++ | The operand is incremented by 1. |
Decrement | — | x– | The operand gets decremented by 1. |
Modulus | % | x % y | Divides left operator by right operator and returns remainder. |
2). Relational Operators
These operators are used to draw comparison between two operands.
They check values such as greater than, less than, equal to, etc and return the answer as TRUE (1) or FALSE (0).
You need to be very careful in selecting “=” or “==” as both of them convey different meaning.
“=” comes under the category of assignment operators and “==” is used when we are comparing two values whether they are equal or not.
Relational Operators | |||
Operation | Symbol | Usage | Explanation |
Equal to | == | x == y | Returns TRUE if first operand (x) is equal to second operand (y). |
Not equal to | != | x != y | Returns TRUE when first operand (x) is not equal to second operand (y). |
Greater than | > | x > y | Return TRUE when first operand (x) is greater than second operand (y). |
Less than | < | x < y | Return TRUE when x is less than y. |
Greater than equal to | >= | x >= y | Return TRUE when x is greater than or equal to y. |
Less than equal to | <= | x <= y | Return TRUE when x is less than or equal to y. |
3). Logical Operators
This operator deals with logical operations performed on the Boolean values of the operators. It includes Logical AND, OR and NOT operators.
In C/C++, a non-zero number is considered as BOOLEAN TRUE or 1. If it’s a character or a special character, the corresponding ASCII Values are taken.
This may hold true for certain other languages.
Logical Operators | |||
Operation | Symbol | Usage | Explanation |
Logical OR | || | x || y | Returns TRUE (1) if any of the operand is TRUE, else returns FALSE or 0. |
Logical AND | && | x && y | Returns TRUE (1) when both the operands are TRUE else returns FALSE (0). |
Logical NOT | ! | !x | This is a unary operator and returns TRUE (1) if the operand is FALSE (0) and vice versa. |
4). Assignment Operators
This operator deals with logical operations dealing with Boolean values of the operators. It includes Logical AND, OR and NOT operators.
In C/C++, a non zero number is considered as BOOLEAN TRUE or 1. If it’s a character or a special character, the corresponding ASCII Values are taken.
This may hold true for certain other languages.
Assignment Operators | |||
Operation | Symbol | Usage | Explanation |
Value assigning | = | x = y | Here the value of right operator (y) is assigned to left operator (x). |
Add and assign | += | x += y | x= x + y; Here both the operands are added and the value is assigned back to first operand. |
Subtract and assign | -= | x -= y | x= x – y; The second operand is subtracted from first and the result is assigned to first operand. |
Multiply and assign | *= | x *= y | x = x * y; Both the operands are multiplied and the result is then assigned to the first operand. |
Divide and assign | /= | x /= y | x = x / y; First operand is divided by second and the quotient is assigned to the first operand. |
Modulus and assign | %= | x %= y | x = x % y; First operand is divided by second and the remainder is assigned to the first operand. |
5). Bitwise Operators
These are special types of operators which convert the decimal representation of the number to binary ad then perform the operation bit by bit.
The final result is again converted back into decimal number.
The main advantage of these functions is that they boost the processing speed as well efficiency of the program.
Bitwise Operators | |||
Operation | Symbol | Usage | Explanation |
Bitwise OR | & | x | y | Both the operands x and y are converted into binary. The OR operation is performed bit by bit. The final answer is again converted back into decimal number. |
Bitwise AND | | | x & y | Both the operands x and y are converted into binary. The AND operation is performed bit by bit. The final answer is again converted back into decimal number. |
Bitwise Exclusive OR | ^ | x ^ y | Exclusive OR operation is performed but by bit after converting the operands into binary system. |
Complement | ~ | ~x | It’s a unary operator. All the bits are complemented to deliver the final answer. |
Left Shift | << | x << y | Here the left bits of the operand (x) are moved as specified by the second operand (y). It is more like multiplying x with 2y. |
Right Shift | >> | x >> y | Here the right bits of the operand are moved specified by the operand y. It’s like dividing x by 2y. |
Reference – If you want rendering services then click here.
Read More – Coding and Programing Questions
View More – Useful links for Your Child’s Development