Operators in C++ | Explained Simply

Hello Readers,

Today, we’ll be going to discuss the operators in C++. In the previous tutorial, we discussed the data types in C++. Here is the link to practice C++ questions. Here is the link to the external documentation, if you want to have a look at it.

Operators are those symbols that are used to operate the variables and constants. C++ has a rich set of built-in operators. There are various types of operators:

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

C++ also has the facility of operator overloading, in which we overload the build-in or pre-defined operators. We’ll see operator overloading in the next upcoming tutorials.

Before starting with the explanation of types of operators, I would like to define a technical terminology here: Operand

Operand: Operand is a variable or constant which is used with the operator to get the desired result.

Arithmatic Operator in C++

An arithmetic operator is an operator which does arithmetic operations on the variables & constants. Below is the list of important arithmetic operators:

For Example: Let’s say A = 20 and B = 10

OperatorDescriptionExample
+Adds 2 operandsA + B = 30
Subtract 2 operandsA – B = 10
*Multiply 2 operandsA * B = 200
/Divides 2 operandsA / B = 2
%Modulus Operator calculates the remainder0; Since B divides A completely
++Increment Operator: Increments the integer by 1A++ = 21;
Decrement Operator: Decreases the integer by 1A– = 19;

Related: Modifiers and Datatypes in C++

Relational Operators in C++

Relational Operators are the operators which are used to check the relationship between 2 operands. There are the following relational operators:

For Example: A = 20, B = 10

OperatorDescriptionExample
==Returns true, if both operands are equal, otherwise, return falseA == B returns false
!=Returns false, of both operands, are equal, otherwise returns trueA != B returns true
Returns true if the first operand is greater than second, otherwise, return falseA > B, returns True
Returns false if the first operand is greater than the second, otherwise returns trueA < B, returns False
>=Returns true if the first operand is greater or equal to than second, otherwise returns falseA >= B returns true
<=Returns true if the first operand is smaller or equal to than second, otherwise returns trueA <= B returns false

Related: Variables & its types in C++

Logical Operators in C++

Logical Operators are the operators, which are used to determine the logic between the operands. There are the following logical operators in C++:

For Example, A = 0, B = 1

OperatorDescriptionExample
&&AND Operator. This operator returns true if both the operands are not zero, otherwise returns falseA && B returns false
||OR Operator. This operator returns false if any of the operands are non-zero, otherwise returns trueA || B returns true
!NOT Operator. This operator returns the opposite of the logical state of the operand.! A returns true

Related: What to learn Basics of C++?

Bitwise Operator in C++

Bitwise operators are the operators which perform the bitwise operation on the operands.

PQP & QP | QP ^ Q
00000
01011
10010
11111

The above example is not showing all the bitwise operators. Below is a detailed description of all the bitwise operators supported in C++:

Assume A = 0011 1100

                B = 0000 1101

OperatorDescriptionExample
&Performs bitwise AND on every bit of 2 numbersA & B = 0000 1100, which is 12
|Performs bitwise OR on every bit of 2 numbersA | B = 0011 1101, which is
^Performs XOR operationA ^ B = 0011 0001
~Bitwise NOT. Inverts all the bits of a number~A = 11000011
<< Left Shift. Takes 2 numbers, left shifts the bits of the first operand, & the number of shifts is decided by the second numberA >> 2 = 0000 1111 = 15
>> Right Shift. Takes 2 numbers, right shifts the bits of the first operand, & the number of shifts is decided by the second numberA << 2 = 1111 0000 = 240

Related: HackerRank is important for placements

Assignment Operator in C++

Assignment variables are the operators, which are used to assign value to a variable. The value of the right side operand is assigned to the left side operand.

So, that’s all about the operators in C++. Hope you enjoy it and more importantly you understood it. If you have any doubts, you can post them in the comments section. Don’t forget to share it with your friends. The next tutorial will be out soon. Till then, stay tuned.

Leave a Reply

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

Share via
Copy link
Powered by Social Snap