C++ Operators

What is Operators?

Operators are special type of functions that takes one or more arguments and produces a new value. Operator is a special symbol that tells the compiler to perform specific mathematical or logical Operation.
1.    Mathematical Operators
2.    Shift Operators
3.    Comma Operators
4.    Unary Operators
5.    Relational Operators
6.    Logical Operators
7.    Bitwise Operators
8.    Assignment Operators
9.    Ternary or Conditional Operators

1. Mathematical Operators

There are operators used to perform basic mathematical operations. Addition (+), subtraction (-), diversion (/) multiplication (*) and modulus (%) are the basic mathematical operators etc.
Example: If A=5 and B=10

Example
Result
Operator
A+B
12
+
A/B
2
/
A%B
0
%

2. Shift Operators

Shift Operators are used to shift Bits of any variable. There are following Types of shift operators,
·         Left Shift Operator <<
·         Right Shift Operator >>
·         Unsigned Right Shift Operator >>>

Example: If A= 110101000101010 and B=1

Example
Result
Operator
A>>B
011010100010101
>> 

Explain: This says that it's defined to write a >> b when a is an unsigned int. As you shift right, the least significant bits are removed, other bits are shifted down, and the most significant bits become zero


3. Comma Operators

This is used to separate variable names and to separate expressions.
Example: If int a,b,c;

Example
Result
Operator
a=b++, c++;
a = c++ will be done
,

4. Unary Operators

These are the operators which work on only one operand. There are many unary operators, but increment ++ and decrement -- operators are most used.

For examples

·         unary minus(-)
·         increment(++)
·         decrement(- -)
·         NOT(!)
·         Addressof operstor(&)
·         sizeof()

Example: If a=3;

Example
Result
Operator
b = -a;
-3
-
b=++a
4
++ prefix
b=a++
c=a
3
4
++ postfix

5. Relational Operators

These operators establish a relationship between operands. The relational operators are: less than (<), grater than (>), less than or equal to (<=), greater than equal to (>=), equivalent (==) and not equivalent (! =)

Example: If A=5 and B=4

Example
Result
Operator
B<A
true
< 
B==A
false
==


6. Logical Operators

The logical operators are AND (&&) and OR (||). They are used to combine two different expressions together.

Example: If A=5, B=4, C=11

Example
Result
Operator
B<A && C=A
false
&&
B<A || C=A
true
||

7. Bitwise Operators

Bitwise operator works on bits and perform bit-by-bit operation. 

Example: If A= 0011 1100 and B= 0000 1101

Example
Result
Operator
A&B
0000 1100
&
A|B
0011 1101
|
A^B 
0011 0001
^

8. Assignment Operators

Which can be used to assign a value to a variable.

Example: Let’s suppose variable A=8 and B=3.

Example
Result
Operator
A+=B or A=A+B
11
+=
A-=3 or A=A+3
5
-=

9. Ternary or Conditional Operators

The ternary if-else? : is an operator which has three operands.
Example: Let’s suppose variable A=10

Example
Result
Operator
a > 5 ?
11
?