January 13, 2021

C++ Operators

In this tutorial, you will learn about the various operators available in C++. An operator is a symbol that tell the compiler to perform certain operation on variables. Let’s consider the following six categories of operators:

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Other Operators

 

1. Arithmetic Operators

These are operators used to perform arithmetic operations. For example if x = 10 and y = 20, then the table below show the results of various arithmetic operators

OperatorBrief DescriptionExample
Addition (+)Adds two operandsx + y will give 30
Subtraction(-)Subtracts second operand from the firstx – y will give -10
Multiplication(*)Multiplies both operandsx * y will give 200
Division(/)Divides numerator by de-numeratory / x will give 2
Modulus(%)Gives remainder of after an integer divisiony % x will give 0
Increment(++)Increases integer value by onex++ will give 11
Decrement(–)Decreases integer value by onex– will give 9

 

2. Relational Operators

These are sometimes called comparison operators. They compare two operand and returns either true or false based on the result. So if x = 10 and y = 10, then the table below show the results of various relational operators

OperatorBrief DescriptionExample
==Checks whether the values of two operands are equal, if yes then the condition becomes true.(x == y) is not true.
!=Checks whether the values of two operands are equal, if values are not equal then the condition becomes true.(x != y) is true.
>Checks whether the value of left operand is greater than the value of right operand, if so, then the condition becomes true.(x > y) is not true.
<Checks whether the value of left operand is less than the value of right operand, if so, then the condition becomes true.(x < y) is true.
>=Checks whether the value of left operand is greater than or equal to the value of right operand, if so then the condition becomes true.(x >= y) is not true.
<=Checks whether the value of left operand is less than or equal to the value of right operand, if yes then the condition becomes true.(x <= y) is true.

 

3. Logical Operators

These are used to do logical comparison of two operands. If x = 1 and y = 0, then:

OperatorBrief DescriptionExample
Logical AND(&&)If both of the operands are non-zero, then the condition becomes true.(x && y) is false.
Logical OR(||)If any of the two operands is non-zero, then condition evaluates to true.(x || y) is true.
:Logical NOT(!)Use to negate the logical state of its operand. If a condition is true, then the operator will make it false.!(x && y) is true.

 

4. Bitwise Operators

These set of operators are used perform operations on individual bits. They are also called binary operators or bit-by-bit operators.

For instance, let x = 60; and y = 13; converting to  binary format they will be as follows −

x = 0011 1100

y = 0000 1101

—————–

x & y = 0000 1100

x | y = 0011 1101

x ^ y = 0011 0001

~x  = 1100 0011

The table below summarizes bitwise operators.

OperatorBrief DescriptionExample
Binary AND(&)copies a bit to the result if it exists in both operands.(x & y) will give 12 which is 0000 1100
Binary OR(|)copies a bit if it exists in either operand.(x | y) will give 61 which is 0011 1101
Binary XOR(^)copies the bit if it is set in one operand but not both.(x ^ y) will give 49 which is 0011 0001
Binary 1s complement(~)a unary and has the effect of ‘flipping’ bits.(~x ) will give -61 which is 1100 0011 in 2’s complement form due to a signed binary number.
Binary Left Shift(<<)shifts the left operands value left by the number of bits specified by the right operand.x << 2 will give 240 which is 1111 0000
Binary Right Shift(>>)shifts the left operands value right by the number of bits specified by the right operand.x >> 2 will give 15 which is 0000 1111

 

5. Assignment Operators

These are simply used to assign values to variables. Assignment operators are summarized in the table below

OperatorBrief DescriptionExample
Simple Assignment(=)assigns values from right side operands to left side operand.z = x + y will assign value of x + y into z
Add AND Assignment(+=)adds right operand to the left operand and assign the result to left operand.z += x is equivalent to z = z + x
Subtract AND Assignment(-=)subtracts right operand from the left operand and assign the result to left operand.z -= x is equivalent to z = z – x
Multiply AND Assignment(*=)multiplies right operand with the left operand and assign the result to left operand.z *= x is equivalent to z = z * x
Divide AND Assignment(/=)divides left operand with the right operand and assign the result to left operand.z /= x is equivalent to z = z / x
Modulus AND Assignment(%=) takes modulus using two operands and assign the result to left operand.z %= x is equivalent to z = z % x
Left Shift AND Assignment(<<=)z <<= 2 is same as z = z << 2
Right Shift AND Assignment(>>=)z >>= 2 is same as z = z >> 2
Bitwise AND Assignment(&=)z &= 2 is same as z = z & 2
Bitwise XOR Assignment(^=)z ^= 2 is same as z = z ^ 2
Bitwise OR Assignment(|=)z |= 2 is same as z = z | 2

 

6. Other Operators

There are  a few other operators which does not fall under any of the categories discussed. So we group them as ‘other operators’ or ‘misc operators’. They are listed in the table below:

SNOperator  and Description
1sizeof operator

returns the size of a variable. For instance, sizeof(x), where ‘x’ is integer, and will return 4.

2Conditional operator(a ? b : c

If a is true then it returns value of b else, returns value of c

3Comma (,)

causes a sequence of operations to be performed. The value of the entire expression becomes the value of the last expression of the comma-separated list.

4Member operators . (dot) and -> (arrow)

they are used to reference individual members of classes, structures, and unions.

5Casting operators

converts one data type to another. For instance, int(12.75) would return 12. See Data Type Conversion

6Pointer operator &

returns the address of a variable. For instance &a; will give actual address of the variable.

7Pointer operator *

* is pointer to a variable. For instance *var; will pointer to a variable var.

 

Leave a Reply

Your email address will not be published. Required fields are marked *