Articles added in Mathematics Lab | Cyber Lab is now open | Astronomy Lab is now open | VLSI Lab is now open | 120 SEO Tips article published | More exciting articles coming soon!




Operators in C++

Once introduced to variables and constants, we can begin to operate with them by using operators. What follows is a complete list of operators. At this point, it is likely not necessary to know all of them, but they are all listed here to also serve as reference. C++ is having very rich set operators. These operators can be classified into 8 categories:


1. Arithmetic operators:

+, -, *, /, % (mod or remainder operator).

note These operators can be used with any type of operands. If operands are of the same type, then the result will be of the same type except for characters. If operators are characters then the result will be of 'int' type. If operands are of different types then the lower type will be converted into higher type and the result will be of higher type.

Remember precedence of variable type is int < float < double For e.g.:
  • 2 + 5 = 7 (int)
  • 2 + 5.0 = 2.0 + 5.0 = 7.0 (float)
  • 5/2 = 2 (int)
  • 5.0/2 = 5.0/2.0 = 2.5 (float)

Type casting: Using type casting, we can convert the type of one value from its type to some other type. It is done as follows:

Syntax: (type) value
  • Let a = 5, b = 2, c = 3
    (a/b)*c=(5/2)*3 = 2*3 = 6
    But,
    ((float)5/2)*3 = (5.0/2)*3 = 2.5*3 = 7.5


  • Let a = 3.7, b = 2
    a+b = 3.7 + 2 = 5.7
    But,
    (int)a + b = 3 + 2 = 5



% (Mod operator)

It is used to find the remainder after an integer division
Remember, with this operator float and double operands should never be used.

  • 5%2 = 1
  • 7%7 = 0
  • 3%4 = 3
  • -5%2 = -1
  • -5%-2 = -1 (Sign of the result is always sign of the first operand)
  • 5%-2 = 1

Arithmetic operators are used for creating arithmetic expressions.



2. Relational operators:

  • < (Less than)
  • > (Greater than)
  • <= (Less than or equal to)
  • >= (Greater than or equal to)
  • == (Equal to)
  • != (Not equal to)

These operators are used for comparing two quantities, the result of the operation will be either 1 (boolean true) or 0 (boolean false).

Let a=4, b=7
  • a < b --> 1
  • a > b --> 0
  • a <= b --> 1
  • a >= b --> 0
  • a == b --> 0
  • a != b --> 1

Using relational operators, we can create relational expressions.



3. Logical operators

  • && (and)
  • || (or)
  • ! (not)

These operators are used for combining two or more relational expressions. The result of operation will be either 1 (true) or 0 (false).

Let a=2, b=3, c=5
  • a < b && b > c = 0
  • a < b || b < c = 1

! (not operator)
This is a unary operator and is used for negating the value of a relational expression.
Let a=1, b=2
!(a>b) = !0 = 1


With logical operators, we can use any kind of expressions. If the expressions are not relational, then the system will check the value of expression is 0 or non-zero and the non-zero quantity will be treated as 1.

Let a=5, b=2, c=5
  1. a+b && a-c = 5+2 && 5-5 = 7 && 0 = 1 && 0 = 0.
  2. !a = !5 = !1 = 0
  3. a && b --> 5 && 2 --> 1 && 1 --> 1

Using logical operators, we can create logical expressions.



4. Assignment operators


=, +=, -=, *=, /=, etc.

'=' is basic assignment operator. It is used to assign a value to a variable.
Syntax: variable_name = an_expression;

Value of expression is assigned to the variable on left hand side.

  • a=5;
  • a=b;
  • a=b+c;

Other assignments operators - +=, -=, *=, /=, %=, etc.
To understand the concept of these operators, consider the example below:
  • a+=5 means a=a+5
  • a-=3 means a=a-3
  • a*=2 means a=a*2
  • a/=3 means a=a/3
  • a%=5 means a=a%5

Using assignment operators, we can create assignment expressions.



5. Increment and decrement operator:


++, --

Both are unary operators. Both can be used before or after the variable.
++ (increments value of variable by 1)
-- (decrements value of variable by 1)

Consider the operator ++. The same concept is applicable to --

Syntax: ++ variable means pre-increment
variable ++ means post-increment
For example, Let a=5, for ++a or a++ means a=6.


If pre-increment or post-increment is used with a single variable, then their effects are same. If these operators are used in other expressions then their behavior is different. If there is a pre-increment in an expression, then initially the value of that variable will be incremented by 1 and the new value will be used in the expression. If there is a post-increment in an expression, then present value of that variable will be used in that expression. After the evaluation of the expression, the value of the variable will be incremented.

Let a=5
i) Value of equation --> ++a + 3 = 6+3 = 9
Value of variable after execution --> a=6

ii) Value of equation --> b= ++a = 6
Value of variable after execution --> a=6

iii) Value of equation --> b= a++ = 5
Value of variable after execution --> a=6



6. Bitwise operators:


<< shift left
>> shift right
& bitwise and
| bitwise or
^ bitwise xor
~ bitwise not, i.e. 1's complement.

<< shift left operator:
Syntax: a< Where a and n both are integers.
'n' bits of a are shifted towards left by 'n' positions. Right most 'n' positions are filled with 0's and the left 'n' bits will be dropped.
e.g. Let int a=7
a= 0000 0000 0000 0111
a<<1 --> 0000 0000 0000 1110 = 14 = 7*21
a<<2 --> 0000 0000 0001 1100 = 28 = 7*22
a<<3 --> 0000 0000 0011 1000 = 56 = 7*23
i.e. a << n = a*2^n

>> shift right operator:
Syntax: a>>n
Like the above case, here, bits of a will be shifted towards right by 'n' positions. The leftmost 'n' positions will be filled with 0's and the rightmost 'n' bits will be dropped.
e.g. Let int a=9
a= 0000 0000 0000 1001
a>>1 --> 0000 0000 0000 0100 = 4 = 9/21 (exclude the remainder).
a>>2 --> 0000 0000 0000 0010 = 2 = 9/22
a>>3 --> 0000 0000 0000 0001 = 1 = 9/23
i.e. a >> n = a/2^n


& , | , ^ bitwise logical operators:
Syntax: a & b, etc.
These operators do bitwise and, or and xor operations.
e.g. Let a=7 and b=8
a= 0000 0000 0000 0111
b= 0000 0000 0000 1000
a&b --> 0000 0000 0000 0000 = 0
a | b --> 0000 0000 0000 1111 = 15
a ^ b --> 0000 0000 0000 1111 = 15

~ complement operation:
To find 1's complement of a number. This is an unary operator.
e.g. Let a=7
a = 0000 0000 0000 0111
~a = 1111 1111 1111 1000
As the first bit is 1, the number is therefore negative and in 2's complements form. To find the
actual value, we have to find 2's complement.
Therefore, 2's complement = 0000 0000 0000 1000 = 8
Therefore ~a= -8.



7. Conditional operators:


?:

This is a ternary operator which will be normally used for replacing a conditional instruction. Syntax: expression1 ? expression2 : expression3 Here, expression1 is a logical expression i.e. with value 1 or 0, and expression2 and expression3 can be any expressions. Initially expression1 will be evaluated. If its value is 1, then value of complete expression is value of expression2, otherwise it will be value of expression3.

e.g. Let a=5, b=2
a>b ? a+b : a-b --> value = 7.



8. Special operators:


, (comma operator)

Syntax: expression1, expression2, expression3,..., expressionn.

This is an expression known as comma expression. Such an expression will be always evaluated from left to right and the value of expression will be the value of rightmost sub-expression, i.e. 'expressionn'.

e.g. d= a=2, b=3, c=1, a+b+c --> d=6





Precedence and Associativity of operators:

For evaluating an expression that contains more than one operator, for each operator, a rank is assigned. This is known as the precedence of operators. In an expression, if there is more than one operator, than normally, the operator with the highest precedence will be evaluated first, after this, the operator with next higher precedence and so on.
If there are more than one operator with the same precedence, than for some operators, the evaluation will be done from left to right and for some other operators, it will be done from right to left. This is known as associativity of operators.

The principle of Precedence and Associativity of operators is summarized in the table below:















CACKLE comment system





Programming Resources
Computer Networking Fundamentals Android Application