Categories: C++

Operators in C++

An operator is a symbol that instructs the compiler to perform particular mathematical or logical operations. C++ has a wide range of built-in operators, including the following:

Types of operators:
  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operator
  • Unary operator
  • Ternary or Conditional Operator
  • Misc Operator
Arithmetic Operators:

These operators are used on the operands to perform arithmetic or mathematical operations. For example, ‘+’ is used for addition, ‘-‘ for subtraction, ‘*’ for multiplication, and so on. Arithmetic Operators can be further classified into 2 Types:

  • Unary Operators– These operators operate or work with a single operand.
  • Binary Operators– These operators operate or work with two operands.
OperatorDescriptionExample
A=10 B=20
+Adds two operandsA + B will give 30
Subtracts the second operand from the firstA – B will give -10
*Multiplies both operandsA * B will give 200
/Divides numerator by de-numeratorB / A will give 2
%Modulus Operator and the remainder of after an integer divisionB % A will give 0
++Increment operator increases integer value by oneA++ will give 11
The decrement operator decreases integer value by oneA- – will give 9
Relational Operators:

The values of two operands are compared using these operators. For example, ‘>’ determines whether one operand is greater than the other, and so on. The outcome is a Boolean value, either true or false.

OperatorDescriptionExample
A=10 B=20
==Checks whether the values of two operands are equal; if they are, the condition is satisfied.(A == B) is not true.
!=Checks whether the values of two operands are equal; if the values are not equal, the condition is true.(A != B) is true.
>If the value of the left operand is greater than the value of the right operand, the condition is true.(A > B) is not true.
<If the value of the left operand is less than the value of the right operand, the condition is true.(A < B) is true.
>=If the value of the left operand is greater than or equal to the value of the right operand, the condition is true.(A >= B) is not true.
<=If the value of the left operand is less than or equal to the value of the right operand, the condition is true.(A <= B) is true.
Logical Operators:

These operators are used to combine two or more conditions or constraints, as well as to supplement the evaluation of the original condition in question. The outcome is a Boolean value, either true or false.

OperatorDescriptionExample
A=1 B=0
&&Known as the Logical AND operator. If both operands are non-zero, the condition is satisfied.(A && B) is false.
||Known as the Logical OR Operator. If either of the two operands is non-zero, the condition is satisfied.(A || B) is true.
!Called the Logical NOT Operator. Use to reverse the logical state of its operand. If a condition is true, the Logical NOT operator returns false.!(A && B) is true.
Bitwise Operators:

These operators are used on the operands to perform bit-level operations. The operators are first converted to bit-level, and then the operands are calculated. For faster processing, mathematical operations such as addition, subtraction, multiplication, and so on can be performed at the bit level. For instance,

OperatorDescriptionExample
A=60 B=13
&If a bit exists in both operands, the binary AND operator copies it to the result.(A & B) will give 12 which is 0000 1100
|If a bit exists in both operands, the binary OR operator copies it.(A | B) will give 61 which is 0011 1101
^If a bit is set in one operand but not both, the binary XOR operator copies it.(A ^ B) will give 49 which is 0011 0001
~Binary Ones Complement Operator is a unary operator that ‘flips’ bits.(~A ) will give -61 which is 1100 0011 in 2’s complement form due to a signed binary number.
<<Left Shift Binary Operator -The value of the left operand is shifted left by the number of bits specified by the right operand.A << 2 will give 240 which is 1111 0000
>>Right Shift Binary Operator The value of the left operand is shifted right by the number of bits specified by the right operand.A >> 2 will give 15 which is 0000 1111
Assignment Operator:

These operators are used to give a variable a value. The assignment operator’s left operand is a variable, and the assignment operator’s right operand is a value.

OperatorDescriptionExample
=In a straightforward assignment operator, Values are assigned from the right side operands to the left side operands.C = A + B will assign value of A + B into C
+=Add the assignment operator AND, It adds the right operand by the left operand and assigns the result to the left operand.C += A is equivalent to C = C + A
-=Subtract AND assignment operator, It subtracts the right operand from the left operand and assigns the result to left operand.C -= A is equivalent to C = C – A
*=Multiply AND assignment operator, It multiplies right operand with the left operand and assigns the result to left operand.C *= A is equivalent to C = C * A
/=Divide AND assignment operator divides left operand with the right operand and assigns the result to left operand.C /= A is equivalent to C = C / A
%=Modulus AND assignment operator, It takes modulus using two operands and assigns the result to left operand.C %= A is equivalent to C = C % A
<<=Left shift AND assignment operator.C <<= 2 is same as C = C << 2
>>=Right shift AND assignment operator.C >>= 2 is same as C = C >> 2
&=Bitwise AND assignment operator.C &= 2 is same as C = C & 2
^=Bitwise exclusive OR and assignment operator.C ^= 2 is same as C = C ^ 2
|=Bitwise inclusive OR and assignment operator.C |= 2 is same as C = C | 2
Ternary or Conditional Operators(?:):

The ternary if-else?: operator takes three operands.

Syntax:

Expression1? Expression2: Expression3
Misc Operators:

Aside from the operators mentioned above, C++ has a few more common operators.

OperatorOperator Description
sizeofsizeof operator returns the size of a variable. For example, sizeof(a), where ‘a’ is integer, and will return 4.
,Comma operator causes a sequence of operations to be performed. The value of the entire comma expression is the value of the last expression of the comma-separated list.
. (dot) and -> (arrow)Member operators are used to reference individual members of classes, structures, and unions.
CastCasting operators convert one data type to another. For example, int(2.2000) would return 2.
&Pointer operator & returns the address of a variable. For example &a; will give actual address of the variable.
*Pointer operator * is pointer to a variable. For example *var; will pointer to a variable var.

Note: also read about the Variables in C++

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago